从Web服务以正确的顺序迭代JSON数据 [英] Iterate Json data from web service in correct order

查看:218
本文介绍了从Web服务以正确的顺序迭代JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务的响应即将到来,数据是 JSON 的形式。

I have a response coming from a web service, data is in JSON form.

JSONObject event:-

{
  "15:00":{"type":1,"status":null,"appointment_id":null}, 
  "16:00":{"type":1,"status":null,"appointment_id":null},
  "17:00":{"type":1,"status":null,"appointment_id":null},
  "18:00":{"type":1,"status":"1","appointment_id":5}
}

我不知道密钥值,它们是随机的。所以,当我通过获取循环使用迭代器中的数据规则hasNext()。它返回的数据,但改变了未来数据的顺序。

I don't know the key values, they are random. So when i iterate the data using iterator by fetching keys and hasNext(). It returns the data but changes the order of coming data.

Iterator AppoinmentIter = event.keys();
while(AppoinmentIter.hasNext()){   
        String appointmentTime = (String)AppoinmentIter.next();   
        JSONObject appointmentDetails = event.getJSONObject(appointmentTime);
 }

我想在它来临的确切顺序的数据。 我查这个<一href="http://stackoverflow.com/questions/6996191/how-to-get-the-elements-in-correct-order-from-iterator">link,它建议使用的LinkedHashMap 。但在这里,他们正在通过插入钥匙的价值。而且我不知道我的数据的密钥。所以,我怎么可以遍历在正确的顺序中的数据。请帮助..

I want the data in exact order in which it is coming. I checked this link, it suggests to use LinkedHashMap. But here they are inserting the value by keys. And i don't know the keys in my data. So how can i iterate the data in correct order. Please help..

推荐答案

维卡斯,据我理解你的问题,我设法获取JSON对象预期的格式,希望这会帮助你。享受!

Vikas, as far as i understood your problem i managed to retrieve json object in expected format, hope this will help you. Enjoy!!

   String s = "{ \"15:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},\"16:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},\"17:00\":{\"type\":1,\"status\":null,\"appointment_id\":null},\"18:00\":{\"type\":1,\"status\":\"1\",\"appointment_id\":5}}";
    try {
        JSONObject jobj = new JSONObject(s);
        Iterator iter = jobj.keys();
        while(iter.hasNext()){   
            String appointmentTime = String.valueOf(iter.next());
            aRRaY.add(appointmentTime);

     }
      Collections.sort(aRRaY); 
      for(String key : aRRaY){
          JSONObject appointmentDetails = jobj.getJSONObject(key);
          System.out.println(key +" ----- "+appointmentDetails);
      }

    }
    catch (JSONException e) {
        e.printStackTrace();
    }

这篇关于从Web服务以正确的顺序迭代JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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