Created
May 11, 2010 00:54
-
-
Save hemantborole/396767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import net.sf.json.JSONArray; | |
import net.sf.json.JSONObject; | |
/** | |
*formatting a JSON response into Map and List | |
*/ | |
public class Json2Java { | |
private static final int TIMEOUT = 5000; // 5 seconds timeout | |
//This is how you use it. | |
public static void main(String a[]) throws Exception { | |
Json2Java j = new Json2Java(); | |
String jsonStr = "{\"id\":101,\"person\":{\"fname\":\"foo\", \"lname\":\"bar\"}}"; | |
Map<String, Object> resp = j.getMap(jsonStr); | |
System.out.println("Response message = " + ((Map)resp.get("person")).get("lname")); | |
} | |
/** | |
* getList provides a List representation of the JSON Object | |
* @param jsonResponse The JSON array string | |
* @return List of JSONObject. | |
**/ | |
protected List<Object> getList(String jsonResponse) throws Exception { | |
List<Object> listResponse = new ArrayList<Object>(); | |
if (jsonResponse.startsWith("[")) { | |
JSONArray jsonArray = JSONArray.fromObject(jsonResponse); | |
toJavaList(jsonArray, listResponse); | |
} else { | |
throw new Exception("MalFormed JSON Array Response."); | |
} | |
return listResponse; | |
} | |
/** | |
* getMap provides a Map representation of the JSON Object | |
* @param jsonResponse The JSON object string | |
* @return Map of JSONObject. | |
**/ | |
protected Map<String, Object> getMap(String jsonResponse ) throws Exception { | |
Map<String, Object> mapResponse = new HashMap<String, Object>(); | |
if (jsonResponse.startsWith("{")) { | |
JSONObject jsonObj = JSONObject.fromObject(jsonResponse); | |
toJavaMap(jsonObj, mapResponse); | |
} else { | |
throw new Exception("MalFormed JSON Array Response."); | |
} | |
return mapResponse; | |
} | |
/** | |
* toJavaMap converts the JSONObject into a Java Map | |
* @param o | |
* JSONObject to be converted to Java Map | |
* @param b | |
* Java Map to hold converted JSONObject response. | |
**/ | |
private static void toJavaMap(JSONObject o, Map<String, Object> b) { | |
Iterator ji = o.keys(); | |
while (ji.hasNext()) { | |
String key = (String) ji.next(); | |
Object val = o.get(key); | |
if (val.getClass() == JSONObject.class) { | |
Map<String, Object> sub = new HashMap<String, Object>(); | |
toJavaMap((JSONObject) val, sub); | |
b.put(key, sub); | |
} else if (val.getClass() == JSONArray.class) { | |
List<Object> l = new ArrayList<Object>(); | |
JSONArray arr = (JSONArray) val; | |
for (int a = 0; a < arr.size(); a++) { | |
Map<String, Object> sub = new HashMap<String, Object>(); | |
Object element = arr.get(a); | |
if (element instanceof JSONObject) { | |
toJavaMap((JSONObject) element, sub); | |
l.add(sub); | |
} else { | |
l.add(element); | |
} | |
} | |
b.put(key, l); | |
} else { | |
b.put(key, val); | |
} | |
} | |
} | |
/** | |
* toJavaList converts JSON's array response into Java's List | |
* @param ar | |
* JSONArray to be converted to Java List | |
* @param ll | |
* Java List to hold the converted JSONArray response | |
**/ | |
private static void toJavaList(JSONArray ar, List<Object> ll) { | |
int i = 0; | |
while (i < ar.size()) { | |
Object val = ar.get(i); | |
if (val.getClass() == JSONObject.class) { | |
Map<String, Object> sub = new HashMap<String, Object>(); | |
toJavaMap((JSONObject) val, sub); | |
ll.add(sub); | |
} else if (val.getClass() == JSONArray.class) { | |
List<Object> l = new ArrayList<Object>(); | |
JSONArray arr = (JSONArray) val; | |
for (int a = 0; a < arr.size(); a++) { | |
Map<String, Object> sub = new HashMap<String, Object>(); | |
Object element = arr.get(a); | |
if (element instanceof JSONObject) { | |
toJavaMap((JSONObject) element, sub); | |
ll.add(sub); | |
} else { | |
ll.add(element); | |
} | |
} | |
l.add(l); | |
} else { | |
ll.add(val); | |
} | |
i++; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment