如果你的数据很简单,你不想要外部依赖,可以使用以下几行代码:
/**
* 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;
}
这些数据
{"name":"John", "age":"30", "car":"a \"quoted\" back\\slash car"}
生成一个包含
{age=30, car=a "quoted" back\slash car, name=John}
这也可以升级为使用未加引号的值…
/**
* 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;
}
为了解决复杂的结构,它变得很难看…
... 对不起! !... 但我忍不住把它编码了^^
这将解析给定的JSON以及更多内容。它产生嵌套的映射和列表。
/**
* 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 (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;
}
yield -如果你打印地图:
{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}]}