Posted In: JSON

How to parse JSON to POJO in Java

 

This example uses general JSONObject or Any object provided by library. To parse JSON to your own application POJO refere this.

 

1. Example will use following Test data

private String testData1() {
	String json = "{" + " \"addressID\": \"12\","
			+ " \"country\": \"India\","
			+ " \"state\": \"Telengana\","
			+ " \"pinCode\": \"400066\"" + "}";
	return json;
}

private String testData2() {
	String json = "{" + "\"addresses\": [" + "{"
			+ " \"addressID\": \"12\","
			+ " \"country\": \"India\","
			+ " \"state\": \"Telengana\","
			+ " \"pinCode\": \"400066\"" + "}," + "{"
			+ "  \"addressID\": \"13\","
			+ "  \"country\": \"India\","
			+ "  \"state\": \"AndhraPradesh\","
			+ "  \"pinCode\": \"500090\"" + "}" + "]" + "}";
	return json;
}

 

2. Using GSON

private void gson1() {
	Gson gson = new Gson();
	Reader reader = new StringReader(testData1());
	Address address = gson.fromJson(reader, Address.class);
	System.out.println("\ngson1()");
	System.out.println(address);
}

private void gson2() {
	Gson gson = new Gson();
	Reader reader = new StringReader(testData2());
	Addresses addresses = gson.fromJson(reader,
			Addresses.class);
	System.out.println("\ngson2()");
	System.out.println(addresses);
}

 

3. Using Jackson 2X

private void jackson2x1() throws Exception {
	ObjectMapper objectMapper = new ObjectMapper();
	Reader reader = new StringReader(testData1());
	Address address = objectMapper.readValue(reader,
			Address.class);
	System.out.println("\njackson2x1()");
	System.out.println(address);
}

private void jackson2x2() throws Exception {
	ObjectMapper objectMapper = new ObjectMapper();
	Reader reader = new StringReader(testData2());
	Addresses addresses = objectMapper.readValue(reader,
			Addresses.class);
	System.out.println("\njackson2x2()");
	System.out.println(addresses);
}

 

4. Using Jackson 1X

private void jackson1x1() throws Exception {
	org.codehaus.jackson.map.ObjectMapper objectMapper = 
			new org.codehaus.jackson.map.ObjectMapper();
	Reader reader = new StringReader(testData1());
	Address address = objectMapper.readValue(reader,
			Address.class);
	System.out.println("\njackson1x1()");
	System.out.println(address);
}

private void jackson1x2() throws Exception {
	org.codehaus.jackson.map.ObjectMapper objectMapper = 
			new org.codehaus.jackson.map.ObjectMapper();
	Reader reader = new StringReader(testData2());
	Addresses addresses = objectMapper.readValue(reader,
			Addresses.class);
	System.out.println("\njackson1x2()");
	System.out.println(addresses);
}

 

5. Using JsonIterator from json iterator library

private void jsonIterator1() throws Exception {
	Address address = JsonIterator.deserialize(testData1(),
			Address.class);
	System.out.println("\njsonIterator1()");
	System.out.println(address);
}

private void jsonIterator2() throws Exception {
	Addresses addresses = JsonIterator
			.deserialize(testData2(), Addresses.class);
	System.out.println("\njsonIterator2()");
	System.out.println(addresses);
}

 

6. Using net.sf.json.JSONArray from net.sf.json-lib library

private void netsfjsonlib1() throws Exception {
	Address address = (Address) JSONObject.toBean(
			JSONObject.fromObject(testData1()),
			Address.class);
	System.out.println("\nnetsfjsonlib1()");
	System.out.println(address);
}

private void netsfjsonlib2() throws Exception {
	Addresses addresses = (Addresses) JSONObject.toBean(
			JSONObject.fromObject(testData2()),
			Addresses.class);
	System.out.println("\nnetsfjsonlib2()");
	System.out.println(addresses);
}

 

7. Output

com.javausecase.hashmap4.Address@6a41eaa2[addressID=12,country=India,state=Telengana,pinCode=400066]

com.javausecase.hashmap4.Addresses@1a968a59[addresses=
[
com.javausecase.hashmap4.Address@4667ae56[addressID=12,country=India,state=Telengana,pinCode=400066],
com.javausecase.hashmap4.Address@77cd7a0[addressID=13,country=India,state=AndhraPradesh,pinCode=500090]
]]

 

Tags: , , , , ,

by , on February 22nd, 2017

  • Categories