安卓:我如何解析这个混合了JSON响应JSON

2023-09-07 10:47:25 作者:陌生的,对白

这是保证始终在那里是messagesByDate obj中的唯一的事情。该阵列并命名,如2012 5月15日的目的是通过基于而与否的消息是该日期present服务器(没有控制)产生。

如果妳发现的第一个日期重新presented是一个数组,而其他日期是包含已编号的其他对象的对象。

问题1:我如何解析这个不知道什么日期将是present

问题2:有些消息是在一个数组,而不是一个对象。我如何把它们放在一起在一个ArrayList中。而当其在数组或不因为数组不会一直存在。

请任何帮助将是AP preciated因为我到我最后的头发

感谢。

  {messagesByDate:{  2012年5月15:     {        ID:1383483367,        conversation_id:274618561,        USER_ID:4318264,        消息:行会做,        DATE_SENT:1337133515,        date_sent_ago:7分钟前     },     {        ID:1380222533,        conversation_id:274618561,        USER_ID:5159567,        消息:确定好HMU        DATE_SENT:1337085122,        date_sent_ago:13小时前     },     {        ID:1380172978,        conversation_id:274618561,        USER_ID:5159567,        消息:超级前端        DATE_SENT:1337083910,        date_sent_ago:13小时前     },     {        ID:1380130860,        conversation_id:274618561,        USER_ID:5159567,        消息:你准备好了B,        DATE_SENT:1337082797,        date_sent_ago:14小时前     },     {        ID:1378841432,        conversation_id:274618561,        USER_ID:5159567,        消息:打我的电池寿        DATE_SENT:1337054524,        date_sent_ago:22小时前     },     {        ID:1378836763,        conversation_id:274618561,        USER_ID:5159567,        消息:怎么了宝贝,        DATE_SENT:1337054475,        date_sent_ago:22小时前     }  ]  2012年5月12:{     6:{        ID:1362948558,        conversation_id:274618561,        USER_ID:4318264,        消息:OK IMA文字U        DATE_SENT:1336819668,        date_sent_ago:4天前     }  },  2012年5月11:{     7:{        ID:1361356267,        conversation_id:274618561,        USER_ID:5159567,        消息:是啊,这就是酷,        DATE_SENT:1336790738,        date_sent_ago:4天前     },     8:{        ID:1357783913,        conversation_id:274618561,        USER_ID:5159567,        消息:对不起,这里将IMü喜欢,而不是交换numebers?        DATE_SENT:1336722533,        date_sent_ago:4天前     },     9:{        ID:1357759262,        conversation_id:274618561,        USER_ID:5159567,        消息:喂?        DATE_SENT:1336721851,        date_sent_ago:4天前     }     }   }} 
Android开发,关于Json的解析

答案SORTA-KINDA

 的JSONObject dateHolder = r.getJSONObject(messagesByDate);    迭代器holderItr = dateHolder.keys();    而(holderItr.hasNext()){        。字符串thisdate = holderItr.next()的toString();        对象date = dateHolder.get(thisdate);        如果(日期的instanceof JSONArray),其中{            的System.out.println(thisdate +是一个数组。);            JSONArray jarray =(JSONArray),其中日期;            为(中间体X = 0; X&下; jarray.length(); X ++){                字符串的msgId = jarray.getJSONObject(X).getString(ID);                字符串msgConvoId = jarray.getJSONObject(X).getString(conversation_id);                字符串msgUserId = jarray.getJSONObject(X).getString(user_ID的);                字符串msgBody = jarray.getJSONObject(X).getString(信息);                字符串msgDateSent = jarray.getJSONObject(X).getString(DATE_SENT);                字符串msgDateSentAgo = jarray.getJSONObject(X).getString(date_sent_ago);                HashMap的<字符串,字符串>临时=新的HashMap<字符串,字符串>();                temp.put(的msgId的msgId);                temp.put(msgUserId,msgUserId);                temp.put(msgBody,msgBody);                temp.put(msgDateSent,msgDateSent);                temp.put(msgDateSentAgo,msgDateSentAgo);                messages.add(临时);            }        }其他{            的System.out.println(thisdate +是一个对象。);            JSONObject的jobj =(JSONObject的)日期;            迭代器insideDate = jobj.keys();            而(insideDate.hasNext()){                。弦数= insideDate.next()的toString();                的System.out.println(数);                字符串的msgId = jobj.getJSONObject(数字).getString(ID);                串msgConvoId = jobj.getJSONObject(数).getString(conversation_id);                串msgUserId = jobj.getJSONObject(数).getString(USER_ID);                串msgBody = jobj.getJSONObject(数).getString(消息);                串msgDateSent = jobj.getJSONObject(数).getString(DATE_SENT);                串msgDateSentAgo = jobj.getJSONObject(数).getString(date_sent_ago);                HashMap的<字符串,字符串>临时=新的HashMap<字符串,字符串>();                temp.put(的msgId的msgId);                temp.put(msgUserId,msgUserId);                temp.put(msgBody,msgBody);                temp.put(msgDateSent,msgDateSent);                temp.put(msgDateSentAgo,msgDateSentAgo);                messages.add(临时);            }        }    } 

这给了我一个HashMap中的所有邮件,并将其添加到所谓的消息像我想要一个ArrayList,但按日期的失灵。 JSON的是由上市日期...任何人都知道,如果有一种方法直接JSON阅读?或者是我while和for循环失灵?我可以排序的关键包含HashMap?我将谷歌认为...

解决方案

  JSONObject的JSON = service.getJunk();JSONObject的msgJson = json.getJSONObject(messagesByDate);对于(迭代它= msgJson.keys(); it.hasNext();){    obj对象= msgJson.get((字符串)it.next());    如果(OBJ的instanceof的JSONObject){       JSONObject的jobj =(JSONObject的)目标文件;       //过程JSON对象    }其他{       JSONArray ARRY =(JSONArray),其中OBJ;       //处理阵列    }} 

The only thing that's guaranteed to always be there is the messagesByDate obj. The array and objects named such as "15 MAY 2012" are generated by a server(no control) based on rather or not messages are present for that date.

If u notice the first date represented is an array while the other dates are objects containing other objects that have been numbered.

QUESTION 1: how do i parse this without knowing what dates will be present?

QUESTION 2: Some messages are in an array instead of an object. how do I put them all together in one ArrayList. Rather its in an array or not because the array will not always been there.

Please any help would be appreciated as I'm down to my last hair

Thanks.

{
"messagesByDate":{
  "15 May 2012":[
     {
        "id":"1383483367",
        "conversation_id":"274618561",
        "user_id":"4318264",
        "message":"ok will do",
        "date_sent":"1337133515",
        "date_sent_ago":"7 mins ago"
     },
     {
        "id":"1380222533",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"ok well hmu",
        "date_sent":"1337085122",
        "date_sent_ago":"13 hrs ago"
     },
     {
        "id":"1380172978",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"superhead",
        "date_sent":"1337083910",
        "date_sent_ago":"13 hrs ago"
     },
     {
        "id":"1380130860",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"you ready B",
        "date_sent":"1337082797",
        "date_sent_ago":"14 hrs ago"
     },
     {
        "id":"1378841432",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"hit my cell tho",
        "date_sent":"1337054524",
        "date_sent_ago":"22 hrs ago"
     },
     {
        "id":"1378836763",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"whats up baby",
        "date_sent":"1337054475",
        "date_sent_ago":"22 hrs ago"
     }
  ],
  "12 May 2012":{
     "6":{
        "id":"1362948558",
        "conversation_id":"274618561",
        "user_id":"4318264",
        "message":"ok ima text u",
        "date_sent":"1336819668",
        "date_sent_ago":"3 days ago"
     }
  },
  "11 May 2012":{
     "7":{
        "id":"1361356267",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"yea thats cool",
        "date_sent":"1336790738",
        "date_sent_ago":"3 days ago"
     },
     "8":{
        "id":"1357783913",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"sorry im here. would u like to exchange numebers instead?",
        "date_sent":"1336722533",
        "date_sent_ago":"4 days ago"
     },
     "9":{
        "id":"1357759262",
        "conversation_id":"274618561",
        "user_id":"5159567",
        "message":"hello?",
        "date_sent":"1336721851",
        "date_sent_ago":"4 days ago"
     }
     }
   }
}

THE ANSWER SORTA-KINDA

JSONObject dateHolder = r.getJSONObject("messagesByDate");
    Iterator holderItr = dateHolder.keys();


    while(holderItr.hasNext()){


        String thisdate = holderItr.next().toString();
        Object date = dateHolder.get(thisdate);


        if (date instanceof JSONArray) {
            System.out.println(thisdate+" is an ARRAY.");
            JSONArray jarray = (JSONArray) date;
            for(int x=0;x<jarray.length();x++){
                String msgId = jarray.getJSONObject(x).getString("id");
                String msgConvoId = jarray.getJSONObject(x).getString("conversation_id");
                String msgUserId = jarray.getJSONObject(x).getString("user_id");
                String msgBody = jarray.getJSONObject(x).getString("message");
                String msgDateSent = jarray.getJSONObject(x).getString("date_sent");
                String msgDateSentAgo = jarray.getJSONObject(x).getString("date_sent_ago");
                HashMap<String,String> temp = new HashMap<String,String>();
                temp.put("msgId",msgId);
                temp.put("msgUserId", msgUserId);
                temp.put("msgBody", msgBody);
                temp.put("msgDateSent", msgDateSent);
                temp.put("msgDateSentAgo", msgDateSentAgo);
                messages.add(temp);

            }
        } else {
            System.out.println(thisdate+" is an OBJECT.");
            JSONObject jobj = (JSONObject) date;
            Iterator insideDate = jobj.keys();
            while(insideDate.hasNext()){
                String number = insideDate.next().toString();
                System.out.println(number);
                String msgId = jobj.getJSONObject(number).getString("id");
                String msgConvoId = jobj.getJSONObject(number).getString("conversation_id");

                String msgUserId =jobj.getJSONObject(number).getString("user_id");

                String msgBody = jobj.getJSONObject(number).getString("message");

                String msgDateSent = jobj.getJSONObject(number).getString("date_sent");

                String msgDateSentAgo = jobj.getJSONObject(number).getString("date_sent_ago");
                HashMap<String,String> temp = new HashMap<String,String>();
                temp.put("msgId",msgId);
                temp.put("msgUserId", msgUserId);
                temp.put("msgBody", msgBody);
                temp.put("msgDateSent", msgDateSent);
                temp.put("msgDateSentAgo", msgDateSentAgo);
                messages.add(temp);

            }
        }
    }

This gives me all the messages in a HashMap and adds it to an ArrayList called messages like I want but its out of order by date. the json is listed by date...anyone know if there is a way to direct json reading? OR are my WHILE and FOR loops out of order? can i sort hashmaps by a key? I'll google that...

解决方案

JSONObject json = service.getJunk();
JSONObject msgJson = json.getJSONObject("messagesByDate");
for( Iterator it = msgJson.keys(); it.hasNext(); ) {
    Object obj = msgJson.get( (String)it.next() );
    if( obj instanceof JSONObject ) {
       JSONObject jobj = (JSONObject)obj;
       // process json object
    } else {
       JSONArray arry = (JSONArray)obj;
       // process array
    }
}