2

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?

Bruce
  • 1,645
  • 3
  • 15
  • 14
  • check using a Gson lib and model class, no manual parsing required then...http://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android/21480997#21480997 – Pararth Apr 15 '14 at 04:47

3 Answers3

3

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
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
3

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):
Hariharan
  • 24,741
  • 6
  • 50
  • 54
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);
                                    }
Yogesh D
  • 1,663
  • 2
  • 23
  • 38