【问题标题】:getting null value while parsing json data in android在android中解析json数据时获取空值
【发布时间】:2017-02-24 05:12:15
【问题描述】:

我正在尝试在 android 应用程序中解析 json 数据我能够获得书名和作者,但我无法解析出版商我在出版商处得到空值

   "kind":"books#volumes",
       "totalItems":997,
       "items":[  
          {  
             "kind":"books#volume",
             "id":"g3hAdK1IBkYC",
             "etag":"o+CdTUx3mkQ",
             "selfLink":"https://www.googleapis.com/books/v1/volumes/g3hAdK1IBkYC",
             "volumeInfo":{  
                "title":"Professional Android 4 Application Development",
                "authors":[  
                   "Reto Meier"
                ],
                "publisher":"John Wiley & Sons",
                "publishedDate":"2012-04-05",
}

这是为 Json 编写的 java 代码

 JSONObject root = new JSONObject(jsonResponse);
            JSONArray itemsArray = root.optJSONArray("items");
            for (int i=0;i<itemsArray.length();i++){

                JSONObject singleItem = itemsArray.getJSONObject(i);


                JSONObject volumeInfo = singleItem.getJSONObject("volumeInfo");
                String title = volumeInfo.getString("title");
                Log.i("title_book",title);

                String author = volumeInfo.getString("authors");
                Log.i("author_book",author);
                String publisher = singleItem.getString("publisher");
                Log.i("publisher_book",publisher);

【问题讨论】:

  • 你的json格式不好,试试格式化,应该可以遍历的
  • 你到底想说什么?我的 json 未在问题或我编写的 java 代码中格式化

标签: android json


【解决方案1】:

似乎publisher 包含在volumeInfo 中。试试:

String publisher = volumeInfo.getString("publisher");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2012-05-01
    • 2020-10-04
    • 2013-03-10
    相关资源
    最近更新 更多