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

JSON to HashMap Recursively

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

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);
}
}
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 + "]";
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Capitalizing the First Letter of a String in JavaScript

Masai Unit 2 Project-

React Native with TypeScript

Saucony Shadow 6000 https://t.co/UrnMX26dpp https://t.co/itEg09btiQ

Angular CLI Fullstack (NodeJS + Express + Mongoose) — Part 4

Laravel Gitlab CI/CD for busy

বুষ্ট জেটপ্যাক ডে-২

Your post has no words but an image. Please add a caption before publishing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

SOLID Principles in Java

TIL 0510 Java Lambda

File handling in java

Exploring What’s Inside java.util.concurrent Package (Part 2)