【问题标题】:Re-sort json output based on values that are inside a jsonObject. Java Android根据 jsonObject 中的值重新排序 json 输出。 Java 安卓
【发布时间】: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


    【解决方案1】:

    你可能做不到。

    对象是一组无序名称/值对。

    http://www.json.org/

    换句话说,根据定义,JSON 元素没有特定的顺序。您可能应该将 JSON 反序列化为一个知道如何打印自身的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-24
      • 2020-06-17
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 2017-05-01
      相关资源
      最近更新 更多