如何排序时间JsonParsing? [英] How to sort Time in JsonParsing?

查看:141
本文介绍了如何排序时间JsonParsing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的时候从JSON提要排序

I need to sort the time from the Json feed

{
    "events":{
        "event":[
        {
        }
        ]
    }
}

谁能帮我解决这个问题?

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"))
    }
});

这篇关于如何排序时间JsonParsing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆