Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JSON double dimensional array in Java

Tags:

java

json

android

Each news entry has three things : title,contentand date.

The entries are retrieved from a database and I would like to read them in my application using JSONObject and JSONArray. However, I do not know how to use these classes.

Here is my JSON string:

[
   {
      "news":{
         "title":"5th title",
         "content":"5th content",
         "date":"1363197493"
      }
   },
   {
      "news":{
         "title":"4th title",
         "content":"4th content",
         "date":"1363197454"
      }
   },
   {
      "news":{
         "title":"3rd title",
         "content":"3rd content",
         "date":"1363197443"
      }
   },
   {
      "news":{
         "title":"2nd title",
         "content":"2nd content",
         "date":"1363197409"
      }
   },
   {
      "news":{
         "title":"1st title",
         "content":"1st content",
         "date":"1363197399"
      }
   }
]
like image 389
Alex Avatar asked Mar 14 '13 14:03

Alex


2 Answers

Your JSON string is a JSONArray of JSONObject which then contain an inner JSONObject called "news".

Try this for parsing it:

JSONArray array = new JSONArray(jsonString);

for(int i = 0; i < array.length(); i++) {
    JSONObject obj = array.getJSONObject(i);
    JSONObject innerObject = obj.getJSONObject("news");

    String title = innerObject.getString("title");
    String content = innerObject.getString("content");
    String date = innerObject.getString("date");

    /* Use your title, content, and date variables here */
}
like image 88
Pragnani Avatar answered Nov 11 '22 21:11

Pragnani


First of all, your JSON structure is not ideal. You have an array of objects, with each object having a single object in it. However, you could read it like this:

JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();

for (int counter = 0; counter < arrayLength; counter ++) {
    JSONObject thisJson = jsonArray.getJSONObject (counter);

    // we finally get to the proper object
    thisJson = thisJson.getJSONObject ("news");

    String title = thisJson.getString ("title");
    String content = thisJson.getString ("content");
    String date = thisJson.getString ("date");

}

However!

You could do better if you change your JSON to look like the following:

[
    {
        "title": "5th title",
        "content": "5th content",
        "date": "1363197493"
    },
    {
        "title": "4th title",
        "content": "4th content",
        "date": "1363197454"
    }
]

Then, you could parse it as follows:

JSONArray jsonArray = new JSONArray (jsonString);
int arrayLength = jsonArray.length ();

for (int counter = 0; counter < arrayLength; counter ++) {
        // we don't need to look for a named object any more
    JSONObject thisJson = jsonArray.getJSONObject (counter);    

    String title = thisJson.getString ("title");
    String content = thisJson.getString ("content");
    String date = thisJson.getString ("date");
}
like image 37
Shade Avatar answered Nov 11 '22 20:11

Shade