Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:48:10+00:00 2026-06-03T22:48:10+00:00

The only thing that’s guaranteed to always be there is the messagesByDate obj. The

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:48:11+00:00Added an answer on June 3, 2026 at 10:48 pm
    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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to override a Java method that the only thing that
GLSL has a full C-style preprocessor. The only thing that does not work is
Strangely I find no support for Midi in Android. The only thing that comes
I have to ask this, because: The only thing I recognize is, that if
I've been working on a Facebook app and the only thing that's still bogging
I'm creating a backgrounded cocoa application. The only thing that it's missing is the
We are using SQL Server 2008 and the only thing that is using it
When I use it, the only thing that is filled is a white rectangle,
I'm on my last steps to open my website, but the only thing that
I have my socket comms pretty much working. The only thing that I'm not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.