【发布时间】:2017-02-24 08:30:03
【问题描述】:
我正在尝试解析和读取我得到的 JSON 文件。我在每个对象中都得到一个“appointmentInstance”,我想根据appointmentInstance对整个文件进行重新排序。例子: 如果 JSONFile 约会实例具有这种结构:
{
response: {
status: 200,
message: "",
details: "",
eventId: 0,
startRow: 0,
endRow: 9,
totalRows: 9,
data: [
{
id: 509955,
startTimeSlot: 5,
appointmentInstance: 310051
},
{
id: 509961,
startTimeSlot: 2,
appointmentInstance: 310057
},
{
id: 510070,
startTimeSlot: 3,
appointmentInstance: 310166
},
{
id: 510074,
startTimeSlot: 4,
appointmentInstance: 310170
},
{
id: 510522,
startTimeSlot: 6,
appointmentInstance: 310419
},
{
id: 510523,
startTimeSlot: 6,
appointmentInstance: 310420
},
{
id: 510524,
startTimeSlot: 7,
appointmentInstance: 310421
},
{
id: 510525,
startTimeSlot: 6,
appointmentInstance: 310060
},
{
id: 510535,
startTimeSlot: 7,
appointmentInstance: 310171
}
]
}
}
然后我希望它被重新排序为:
{
response: {
status: 200,
message: "",
details: "",
eventId: 0,
startRow: 0,
endRow: 9,
totalRows: 9,
data: [
{
id: 509955,
startTimeSlot: 5,
appointmentInstance: 310051
},
{
id: 509961,
startTimeSlot: 2,
appointmentInstance: 310057
},
{
id: 510525,
startTimeSlot: 6,
appointmentInstance: 310060
},
{
id: 510070,
startTimeSlot: 3,
appointmentInstance: 310166
},
{
id: 510074,
startTimeSlot: 4,
appointmentInstance: 310170
},
{
id: 510535,
startTimeSlot: 7,
appointmentInstance: 310171
}
{
id: 510522,
startTimeSlot: 6,
appointmentInstance: 310419
},
{
id: 510523,
startTimeSlot: 6,
appointmentInstance: 310420
},
{
id: 510524,
startTimeSlot: 7,
appointmentInstance: 310421
},
]
}
}
我该怎么做?使用我当前的代码,我可以“打印出”约会实例,但它当然不会按顺序打印。
我的代码:
try {
String thatarray = "{response: {status: 200,message: "",details: "",eventId: 0,startRow: 0,endRow: 9,totalRows: 9,data: [{id: 509955,startTimeSlot: 5,appointmentInstance: 310051},{id: 509961,startTimeSlot: 2,appointmentInstance: 310057},{id: 510070,startTimeSlot: 3,appointmentInstance: 310166},{id: 510074,startTimeSlot: 4,appointmentInstance: 310170},{id: 510522,startTimeSlot: 6,appointmentInstance: 310419},{id: 510523,startTimeSlot: 6,appointmentInstance: 310420},{id: 510524,startTimeSlot: 7,appointmentInstance: 310421},{id: 510525,startTimeSlot: 6,appointmentInstance: 310060},{id: 510535,startTimeSlot: 7,appointmentInstance: 310171}]}}";
JSONObject jsonObject = new JSONObject(thatarray);
jsonObject = jsonObject.getJSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i = jsonArray.length()-1; i>=0; i--){
jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getInt("appointmentInstance"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
【问题讨论】:
标签: java android arrays sorting arraylist