【发布时间】:2017-02-24 07:00:17
【问题描述】:
我正在尝试解析和读取我得到的 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
},
]
}
}
我该怎么做?使用我当前的代码,我可以“打印出”约会实例,但它当然不会按顺序打印。
我的 JSON:
{
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
}
]
}
}
我的 Java Android 代码:
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();
}
}
【问题讨论】:
-
创建一个List,添加
JSONObjects,重写compareTo()方法,对List进行排序,排序。 -
这似乎是一个学校项目,因此,我没有在答案中添加代码。所以,我给了你关于如何尝试解决它的指南......
List是 Java 中的一个类,可以保存Object引用。有一个sort()方法,它调用Object.compareTo(Object)方法,这个方式,你可以谷歌如何简单地做到这一点。并与您的对象有一个有序列表 -
@Bonatti 这不是学校项目 :P,虽然看起来确实像!
标签: java android arrays json sorting