【问题标题】:How to re-sort JSON file based on value inside object Java Android如何根据对象Java Android中的值重新排序JSON文件
【发布时间】: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


【解决方案1】:

像这样使用带有 Collection.sort() 方法的 List

List<Integer> appointments = new ArrayList<Integer>();

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);
        appointments.add(jsonObject.getInt("appointmentInstance"));
    }
    Collections.sort(appointments);
} catch (JSONException e) {
    e.printStackTrace();
}
}

【讨论】:

  • 这很好,但我也希望“id”和“timeslot”(如您在我的 json 中看到的)可以访问
  • 如果您需要更多变量,那么您会将它们包装在一个类中并使用比较器根据您感兴趣的类变量对其进行排序。
【解决方案2】:

首先创建 JSON 响应的模型:

 public class ResponseModel {
        private int id;
        private int startTimeSlot;
        private int appointmentInstance;

      public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

 public void setstartTimeSlot(int startTimeSlot) {
        this.startTimeSlot= startTimeSlot;
    }

    public int getstartTimeSlot() {
        return startTimeSlot
    }

    public void setappointmentInstance(int appointmentInstance){
        this.appointmentInstance= appointmentInstance;
    }

    public int getappointmentInstance(){
        return appointmentInstance;
    }


    }

还有:

try {
        // create a list:
        private ArrayList<ResponseModel> myList = new ArrayList<ResponseModel>();
        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);
            ResponseModel responseModel = new ResponseModel();
            responseModel.setId(jsonObject.getInt("id"));
            responseModel.setstartTimeSlot(jsonObject.getInt("startTimeSlot"));
            responseModel.setappointmentInstance(jsonObject.getInt("appointmentInstance"));            
            myList.add(responseModel);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
    //Sorting:

Collections.sort(myList, new Comparator<ResponseModel >() {
                public int compare(ResponseModel o1, ResponseModel o2) {
                return (o1.getappointmentInstance - o2.getappointmentInstance);
                        }
                });

现在您已经按约会实例对列表 (myList) 进行了排序。

【讨论】:

  • @Jason,我编辑了我的答案。在您的情况下,您可以在没有 compareTo 的情况下比较 compare 方法中的数字。
【解决方案3】:

通过将其添加到 int[] 约会解决

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2021-07-19
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    相关资源
    最近更新 更多