JSONObject 顺序问题

项目返回格式是JSON,用JSONObject解析后发现顺序乱了,原因是JSONObject类用的是HashMap网上说改成改为LinkedHashMap就OK了,因为HaspMap是无序,LinkedHashMap是有序的。改这个要修改源代码,本人不知道怎么修改.自己写的解析JSON格式

代码如下 :

data = data.replace("{", "").replace("}", "").replace("\"", "");
				String[] array = data.split(",");
				for (int i = 0; i < array.length; i++) {
					Map<String, String> itemMap = new TreeMap<String, String>();
					String[] stringArray = array[i].split(":");
					itemMap.put("key", stringArray[0]);
					itemMap.put("value",stringArray[1]);
					list.add(itemMap);

  通过上面解析后stringArray[1]返回的是\u6d4b\u8bd5(测试)就这个问题搞了大半天,new String()方法编码都试变了不行,最后在网上找到代码

 http://www.cnblogs.com/freexiaoyu/archive/2012/06/02/2531815.html

stringArray[1] 改成CharCode.decodeUnicode(stringArray[1]) 显示正常

 

JSONObject jsonObject = new JSONObject(result);
JSONObject indexInfo = jsonObject.getJSONObject("indexInfo");
Iterator<?> itName = indexInfo.keys();
SortedMap map = new TreeMap();
while (itName.hasNext()) {
String strKey = itName.next().toString();
String vlaue = indexInfo.optString(strKey);
map.put(strKey, vlaue);
}
LinkedList itemData = new LinkedList(map.values());

原文地址:https://www.cnblogs.com/freexiaoyu/p/2531910.html