Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: how do i parse this mixed up json response
    primarykey
    data
    text
    <p>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. </p> <p>If u notice the first date represented is an array while the other dates are objects containing other objects that have been numbered. </p> <p>QUESTION 1: how do i parse this without knowing what dates will be present?</p> <p>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. </p> <p>Please any help would be appreciated as I'm down to my last hair</p> <p>Thanks.</p> <pre><code>{ "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" } } } } </code></pre> <p>THE ANSWER SORTA-KINDA </p> <pre><code>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&lt;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&lt;String,String&gt; temp = new HashMap&lt;String,String&gt;(); 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&lt;String,String&gt; temp = new HashMap&lt;String,String&gt;(); temp.put("msgId",msgId); temp.put("msgUserId", msgUserId); temp.put("msgBody", msgBody); temp.put("msgDateSent", msgDateSent); temp.put("msgDateSentAgo", msgDateSentAgo); messages.add(temp); } } } </code></pre> <p>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...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload