9:09 am - Sunday March 26, 2017

Combination of JSON primitives, JSON object and JSON arrays

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

                This program explains about the combination of JSON primitives, json object and JSON arrays.In addition output has been attached below.

Example Program

import java.util.LinkedHashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import org.json.JSONArray;

import org.json.JSONObject;

public class WriteJsontoFile

{

	public static void main(String[] args)

	{

		Map m1 = new LinkedHashMap();

		Map m2 = new HashMap();

		List l1 = new LinkedList();

		m1.put("k11", "v11");

		m1.put("k12", "v12");

		m1.put("k13", "v13");

		m2.put("k21", "v21");

		m2.put("k22", "v22");

		m2.put("k23", "v23");

		l1.add(m1);

		l1.add(m2);

		String jsonString = JSONValue.toJSONString(l1);

		System.out.println(jsonString);

	}
}

Output

                        [{“k11″:”v11″,”k12″:”v12″,”k13″:”v13″},{“k22″:”v22″,”k21″:”v21″,”k23″:”v23″}]

BACK

Filed in: Z-A All Java Codes

Comments are closed.