开发者

How to sort Time in JsonParsing?

开发者 https://www.devze.com 2023-04-07 07:54 出处:网络
I need to sort the time from the Json feed { \"events\":{ \"event\":开发者_JS百科[ { } ] } } can anyone help me to solve this?I don\'t know how you want it to be sorted and how your full data looks

I need to sort the time from the Json feed

{
    "events":{
        "event":开发者_JS百科[
        {
        }
        ]
    }
}

can anyone help me to solve this?


I don't know how you want it to be sorted and how your full data looks like but here's the idea:

JSONArray jsonEvents = new JSONObject(json).getJSONArray("event");
JSONObject [] events = new JSONObject[jsonEvents.length()];

// fill an array with your events
for (int i = 0; jsonEvents.length(); i++) {
    events[i] = jsonEvents.getJSONObject(i);
}

// sort them
Arrays.sort(events, new Comparator<JSONObject>() {
    static final SimpleDateFormat sdfToDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    public int compare(JSONObject event1, JSONObject event2) {
        return sdfToDate.parse(event1.getString("startTime")).compareTo(
            sdfToDate.parse(event2.getString("startTime"))
    }
});


you can use

 Collections.sort(String,new Comparator<String>() {

                                public int compare(String arg0, String arg1) {
                                    // TODO Auto-generated method stub
                                    return 0;
                                }
                            });

All The Best


    String myjson = "{"events":{"event":[{"title":"Audi urban future","startTime":"2011-09-16 00:30:00","endTime":"2011-09-22 00:35:00","description":"test","image":"http://audi.smart-media.no/wp-content/uploads/2011/09/20110919130749-medium.jpg","latitude":45.73154,"longitude":4.8592365}]}}"

    JSONArray array= new JSONArray(myjson);

    for(int i = 0; i < array.length; i++) {
        JSONObject object = (JSONObject) array.get(i);
        object.getString("startTime");
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消