Reading JSON two-dimensional array in Java

Each news item has three contents: title, content and date

To retrieve entries from the database, I want to use jsonobject and jsonarray to read them in my application However, I don't know how to use these classes

This 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"
      }
   }
]

Solution

Your JSON string is the jsonarray of jsonobject, and then contains an internal jsonobject named "news"

Try this to parse 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 */
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>