If your data is simple and you don't want external dependencies, use some lines of code:
/** * A very simple JSON parser for one level, everything quoted. * @param json the json content. * @return a key => value map. */ public static Map<String, String> simpleParseJson(String json) { Map<String, String> map = new TreeMap<>(); String qs[] = json.replace("\\\"", "\u0001").replace("\\\\", "\\").split("\""); for (int i = 1; i + 3 < qs.length; i += 4) { map.put(qs[i].replace('\u0001', '"'), qs[i + 2].replace('\u0001', '"')); } return map; }
So this data
{"name":"John", "age":"30", "car":"a \"quoted\" back\\slash car"}
yields a map containing
{age=30, car=a "quoted" back\slash car, name=John}
This can be upgraded to work with unquoted values too...
/** * A very simple JSON parser for one level, names are quoted. * @param json the json content. * @return a key => value map. */ public static Map<String, String> simpleParseJson(String json) { Map<String, String> map = new TreeMap<>(); String qs[] = json.replace("\\\"", "\u0001").replace("\\\\", "\\").split("\""); for (int i = 1; i + 1 < qs.length; i += 4) { if (qs[i + 1].trim().length() > 1) { String x = qs[i + 1].trim(); map.put(qs[i].replace('\u0001', '"'), x.substring(1, x.length() - 1).trim().replace('\u0001', '"')); i -= 2; } else { map.put(qs[i].replace('\u0001', '"'), qs[i + 2].replace('\u0001', '"')); } } return map; }
And to solve complex structure, it gets ugly... ... SORRY!!! ... but I could not resist to code it^^ This parses the given JSON in question and way more. It yields nested maps and lists.
/** * A very simple JSON parser, names are quoted. * * @param json the json content. * @return a key => value map. */ public static Map<String, Object> simpleParseJson(String json) { Map<String, Object> map = new TreeMap<>(); String qs[] = json.replace("\\\"", "\u0001").replace("\\\\", "\\").split("\""); int index[] = { 1 }; recurse(index, map, qs); return map; } /** * Eierlegende Wollmilchsau. * * @param index index into array. * @param map the current map to fill. * @param qs the data. */ private static void recurse(int[] index, Map<String, Object> map, String[] qs) { int i = index[0]; for (;; i += 4) { String end = qs[i - 1].trim(); // check for termination of an object if (i == qs.length || end.startsWith("}")) { qs[i - 1] = end.substring(1).trim(); i -= 4; break; } String key = qs[i].replace('\u0001', '"'); String x = qs[i + 1].trim(); if (x.endsWith("{")) { x = x.substring(0, x.length() - 1).trim(); if (x.endsWith("[")) { List<Object> list = new ArrayList<>(); index[0] = i + 2; for (;;) { Map<String, Object> inner = new TreeMap<>(); list.add(inner); recurse(index, inner, qs); map.put(key, list); i = index[0]; String y = qs[i + 3]; // check for termination of array if (y.startsWith("]")) { qs[i + 3] = y.substring(1).trim(); break; } } continue; } Map<String, Object> inner = new TreeMap<>(); index[0] = i + 2; recurse(index, inner, qs); map.put(key, inner); i = index[0]; continue; } if (x.length() > 1) { // unquoted String value = x.substring(1, x.length() - 1).trim().replace('\u0001', '"'); if ("[]".equals(value)) // handle empty array map.put(key, new ArrayList<>()); else map.put(key, value); i -= 2; } else { map.put(key, qs[i + 2].replace('\u0001', '"')); } } index[0] = i; }
yields - if you print the map:
{pageInfo={pageName=abc, pagePic=http://example.com/content.jpg}, posts=[{actor_id=1234567890, comments=[], likesCount=2, message=Sounds cool. Can't wait to see it!, nameOfPersonWhoPosted=Jane Doe, picOfPersonWhoPosted=http://example.com/photo.jpg, post_id=123456789012_123456789012, timeOfPost=1234567890}]}
org.jsonis also third party (and unmaintained, it seems). Which libraries are you talking about?