如何用android中的对象键对json对象进行排序?

时间:2013-12-09 10:41:46

标签: android json performance

由于JSON对象不可靠,当我从json文本文件创建一个新的json对象时,顺序出错了。所以我决定重新订购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对象?感谢

2 个答案:

答案 0 :(得分:1)

你可以使这个类具有可比性(基于你的id),然后可以调用Collections.sort(/ 从Json文件中提取的对象列表 /);

答案 1 :(得分:1)

JSON对象没有顺序,它只是一个地图。即使您在文件中按照所需顺序编写JSON,再次加载它也不会有任何顺序概念。

如果您想要订购,则必须使用数组:

[
  {
    "id": "211",
    ...
  },
  {
    "id": "210",
    ...
  },
  ...
]