简体   繁体   中英

How can I parse this JSONArray/JSONObject like this?

I have JSON data like this:

  "books": [
        {
            "rating": {}, 
            "subtitle": "", 
            "author": [
                "The OReilly Java Authors"
            ], 
            "pubdate": "2003-9", 
            "tags": [], 
            "origin_title": "",
               …… …… …… …… … … 

and I want get the author data,some "author"data has more than one author,but I just confused weather I could parse it like this:

JSONArray jsonbooks = mess.getJSONArray("books");
for(blabla){
   JSONObject obj = jsonbooks.getJSONObject(i);
   obj.getJSONArray("author").get(1):

or

  JSONArray jsonbooks = mess.getJSONArray("books");
    for(blabla){
     JSONObject obj = jsonbooks.getJSONObject(i); 
     obj.getString("author");

sencond way,I could get the data like"["author,blabla "],I want get rid off [" "],and I need more string processing,if there a better way looks like the first way?

Since author is an array therefore first convert that into jsonArray

JSONArray jsonAuthor = mess.getJSONArray("author");

then String author = jsonAuthor.get(0);  //this will give you -> The OReilly Java Authors

First one is right just one change obj.getJSONArray("author").getString(0):

JSONArray jsonbooks = mess.getJSONArray("books");
for(blabla){
   JSONObject obj = jsonbooks.getJSONObject(i);
   obj.getJSONArray("author").getString(0):

Hope this helps:

                JSONArray jsonAuthorNode = booksJsonObject.optJSONArray("author");
                int jsonArrLen = jsonAuthorNode.length();

                for(int j=0;j<jsonArrLen;j++)
                {
                    String authorName = jsonAuthorNode.get(j);
                                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM