添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
JSON格式如下: {"username":"tom","age":18,"address":[{"province":"上海市"},{"city":"上海市"},{"disrtict":"静安区" }]} 处理后,得到的结果如下: address |province|city|disrtict|username| age package ai.advance.until; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONArray; public class fastJsonDiff { * 递归读取所有的key * @param jsonObject public static StringBuffer getAllKey(JSONObject jsonObject) { StringBuffer stringBuffer = new StringBuffer(); Iterator <String> keys = jsonObject.keySet().iterator(); // jsonObject.keys(); while (keys.hasNext()) { String key = keys.next(); stringBuffer.append(key.toString()).append( "|" ); if (jsonObject.get(key) instanceof JSONObject) { JSONObject innerObject = (JSONObject) jsonObject.get(key); stringBuffer.append(getAllKey(innerObject)); } else if (jsonObject.get(key) instanceof JSONArray) { JSONArray innerObject = (JSONArray) jsonObject.get(key); stringBuffer.append(getAllKey(innerObject)); return stringBuffer; public static StringBuffer getAllKey(JSONArray json1) { StringBuffer stringBuffer = new StringBuffer(); if (json1 != null ) { Iterator i1 = json1.iterator(); while (i1.hasNext()) { Object key = i1.next(); if (key instanceof JSONObject) { JSONObject innerObject = (JSONObject) key; stringBuffer.append(getAllKey(innerObject)); } else if (key instanceof JSONArray) { JSONArray innerObject = (JSONArray) key; stringBuffer.append(getAllKey(innerObject)); } else { return stringBuffer; private final static String st1 = "{\"username\":\"tom\",\"age\":18,\"address\":[{\"province\":\"上海市\"},{\"city\":\"上海市\"},{\"disrtict\":\"静安区\"}]}" ; private final static String st2 = "{username:\"tom\",age:18}" ; public static void main(String[] args) { System.out.println(st1); JSONObject jsonObject1 = JSONObject.parseObject(st1); StringBuffer stb = getAllKey(jsonObject1); System.err.println(stb);