由於JSON對象不可靠,當我從json文本文件創建新的json對象時,順序出錯。所以我決定重新訂購json對象。如何使用android中的對象鍵排序json對象?
正確的順序(JSON文件內容)
{
"211": {
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/12\/09\/0\/0\/A\/Content\/",
"timestamp": 1386514507,
"cover": ["15\/Pg015.png",
"16\/Pg016.png",
"1\/Pg001.png",
"2\/Pg002.png"],
"year": "2013",
"month": "12",
"day": "09",
"issue": "2013-12-09",
"id": "211"
},
"210": {
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/12\/08\/0\/0\/A\/Content\/",
"timestamp": 1386415087,
"cover": ["1\/Pg001.png",
"2\/Pg002.png",
"3\/Pg003.png",
"4\/Pg004.png"],
"year": "2013",
"month": "12",
"day": "08",
"issue": "2013-12-08",
"id": "210"
},
"209": {
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/12\/07\/0\/0\/A\/Content\/",
"timestamp": 1386331278,
"cover": ["1\/Pg001.png",
"2\/Pg002.png",
"3\/Pg003.png",
"4\/Pg004.png"],
"year": "2013",
"month": "12",
"day": "07",
"issue": "2013-12-07",
"id": "209"
},
"208": {
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/12\/06\/0\/0\/A\/Content\/",
"timestamp": 1386255815,
"cover": ["15\/Pg015.png",
"16\/Pg016.png",
"1\/Pg001.png",
"2\/Pg002.png"],
"year": "2013",
"month": "12",
"day": "06",
"issue": "2013-12-06",
"id": "208"
}
}
......
順序不正確(當我創建一個JSONObject)
{
"211": {
"id": "211",
"timestamp": 1386514507,
"cover": ["15\/Pg015.png",
"16\/Pg016.png",
"1\/Pg001.png",
"2\/Pg002.png"],
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/12\/09\/0\/0\/A\/Content\/",
"issue": "2013-12-09",
"month": "12",
"year": "2013",
"day": "09"
},
"210": {
"id": "210",
"timestamp": 1386415087,
"cover": ["1\/Pg001.png",
"2\/Pg002.png",
"3\/Pg003.png",
"4\/Pg004.png"],
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/12\/08\/0\/0\/A\/Content\/",
"issue": "2013-12-08",
"month": "12",
"year": "2013",
"day": "08"
},
"195": {
"id": "195",
"timestamp": 1385115154,
"cover": ["1\/Pg001.png",
"2\/Pg002.png",
"3\/Pg003.png",
"4\/Pg004.png"],
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/11\/23\/0\/0\/A\/Content\/",
"issue": "2013-11-23",
"month": "11",
"year": "2013",
"day": "23"
},
"194": {
"id": "194",
"timestamp": 1385048906,
"cover": ["1\/Pg001.png",
"2\/Pg002.png",
"3\/Pg003.png",
"4\/Pg004.png"],
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/11\/22\/0\/0\/A\/Content\/",
"issue": "2013-11-22",
"month": "11",
"year": "2013",
"day": "22"
},
"197": {
"id": "197",
"timestamp": 1385306370,
"cover": ["1\/Pg001.png",
"2\/Pg002.png",
"3\/Pg003.png",
"4\/Pg004.png"],
"host": "https:\/\/s3-ap-southeast-1.amazonaws.com\/production-source\/ChangSha\/2013\/11\/25\/0\/0\/A\/Content\/",
"issue": "2013-11-25",
"month": "11",
"year": "2013",
"day": "25"
}
}
.....
我現在有不正確的命令jsonObject。如何用正確的順序創建一個新的json對象?謝謝
謝謝,我決定把json字段放到一個對象中,一個json對象放在一個java對象中,然後放入arraylist
使用自定義比較器:'Collections.sort(objList,new Comparator