JSON to HashMap Recursively and JSON to Object in Java | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

JSON to HashMap Recursively

JsonToHashMap.java

package com.codeFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

/**
 * @author code.factory
 *
 */
public class JsonToHashMap {

	public static void main(String args[]) throws Exception {
		JSONObject jsonObject = new JSONObject(
				"{\"A\":\"M1\",\"Data\":[ {\"B\":[{ \"B1\":\"111\",\"B2\":\"Warning \"},{ \"B1\":\"222\",\"B2\":\"Warning \"}],\"C\":[{\"c1\":\"IL2\",\"c2\":\"[0.750183,0.00933380975964486]\"},{\"c1\":\"IL1b\",\"c2\":\"[0.750183,-1.5216938335421]\"}]}]}");
		System.out.println(getMap(jsonObject));
	}

	private static Map getMap(JSONObject object) {
		Map<String, Object> map = new HashMap<String, Object>();

		Object jsonObject = null;
		String key = null;
		Object value = null;

		try {
			Iterator<String> keys = object.keys();
			while (keys.hasNext()) {

				key = null;
				value = null;

				key = keys.next();

				if (null != key && !object.isNull(key)) {
					value = object.get(key);
				}

				if (value instanceof JSONObject) {
					map.put(key, getMap((JSONObject) value));
					continue;
				}

				if (value instanceof JSONArray) {
					JSONArray array = ((JSONArray) value);
					List list = new ArrayList();
					for (int i = 0; i < array.length(); i++) {
						jsonObject = array.get(i);
						if (jsonObject instanceof JSONObject) {
							list.add(getMap((JSONObject) jsonObject));
						} else {
							list.add(jsonObject);
						}
					}
					map.put(key, list);
					continue;
				}

				map.put(key, value);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return map;
	}
}

JSON to Object

JsonToObject.java

package com.codeFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;

/**
 * @author code.factory
 *
 */
public class JsonToObject {

	public static void main(String[] args) {

		String json = "[{\"fName\": \"Code\",\"lName\": \"Factory\"},{\"fName\": \"Emp Fname\",\"lName\": \"Emp Lname\"}]";
		ObjectMapper mapper = new ObjectMapper();
		List<Employee> list = new ArrayList<Employee>();
		try {
			list = mapper.readValue(json, TypeFactory.defaultInstance().constructCollectionType(List.class, Employee.class));
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println(list);
	}

}

Employee.java

package com.codeFactory;

/**
 * @author code.factory
 *
 */
public class Employee {

	private String fName;
	private String lName;

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	@Override
	public String toString() {
		return "Author [fName : " + fName + ", lName : " + lName + "]";
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s