例如:
String textObject = "{\"row1\":{\"userNmae\":\"zhangsan\"},\"row2\":{\"userNmae\":\"lisi\"},\"row3\":{\"userNmae\":\"wangwu\"}}"; JSONObject json1 = JSONObject.parseObject(textObject); Iterator<String> iterator = json1.keySet().iterator(); int i = 1; while (iterator.hasNext()) { String key = iterator.next(); System.out.println(i + " : " + json1.get(key)); i++; }
想要结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"lisi"} 3 : {"userNmae":"wangwu"}
实际结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"wangwu"} 3 : {"userNmae":"lisi"}
转换时加上:Feature.OrderedField
例如:
String textObject = "{\"row1\":{\"userNmae\":\"zhangsan\"},\"row2\":{\"userNmae\":\"lisi\"},\"row3\":{\"userNmae\":\"wangwu\"}}"; int i = 1; JSONObject json2 = JSONObject.parseObject(textObject, Feature.OrderedField); Iterator<String> iterator2 = json2.keySet().iterator(); while (iterator2.hasNext()) { String key = iterator2.next(); System.out.println(i + " : " + json2.get(key)); i++; }
想要结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"lisi"} 3 : {"userNmae":"wangwu"}
实际结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"lisi"} 3 : {"userNmae":"wangwu"}
根据JSONObject拓展一下:假如JSONArray中包含JSONObject,在调用“JSONObject.parseArray()”时也无法保证顺序。
例如:
String textArray = "[{\"people\":{\"row1\":{\"userNmae\":\"zhangsan\"},\"row2\":{\"userNmae\":\"lisi\"},\"row3\":{\"userNmae\":\"wangwu\"}}}]"; JSONArray array1 = JSONObject.parseArray(textArray); for (int i = 0; i < array1.size(); i++) { JSONObject jsonObject = array1.getJSONObject(i); Iterator<String> iterator = jsonObject.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); JSONObject inner = jsonObject.getJSONObject(key); Iterator<String> innerIter = inner.keySet().iterator(); int flag = 1; while (innerIter.hasNext()) { String innerKey = innerIter.next(); String innerValue = inner.getString(innerKey); System.out.println(flag + " : " + innerValue); flag++; } } }
想要结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"lisi"} 3 : {"userNmae":"wangwu"}
实际结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"wangwu"} 3 : {"userNmae":"lisi"}
解决办法:
参考JSONObject,加 “Feature.OrderedField”,并且转换一下JSONArray的解析方式。
String textArray = "[{\"people\":{\"row1\":{\"userNmae\":\"zhangsan\"},\"row2\":{\"userNmae\":\"lisi\"},\"row3\":{\"userNmae\":\"wangwu\"}}}]"; //不直接转Array,调用parse()方法,当作JSONObject转,然后强制转换。 JSONArray array1 = (JSONArray) JSONObject.parse(textArray, Feature.OrderedField); for (int i = 0; i < array1.size(); i++) { JSONObject jsonObject = array1.getJSONObject(i); Iterator<String> iterator = jsonObject.keySet().iterator(); while (iterator.hasNext()) { String key = iterator.next(); JSONObject inner = jsonObject.getJSONObject(key); Iterator<String> innerIter = inner.keySet().iterator(); int flag = 1; while (innerIter.hasNext()) { String innerKey = innerIter.next(); String innerValue = inner.getString(innerKey); System.out.println(flag + " : " + innerValue); flag++; } } }
实际结果:
1 : {"userNmae":"zhangsan"} 2 : {"userNmae":"lisi"} 3 : {"userNmae":"wangwu"}