【问题标题】:JSON parse sub items without using any lib不使用任何库的 JSON 解析子项
【发布时间】:2020-02-15 23:23:46
【问题描述】:

我有这个 Google 图书 JSON 文本要解析:

   "volumeInfo": {
    "title": "Year Book",
    "subtitle": "The Annual Supplement to the World Book Encyclopedia : the 1989 World Book : a Review of the Events of 1988",
    "authors": [
     "World Book Encyclopedia"
    ],
    "publishedDate": "1989",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "0716604892"
     },
     {
      "type": "ISBN_13",
      "identifier": "9780716604891"
     }
    ],
    "readingModes": {
     "text": false,
     "image": false
    },
    "pageCount": 608,
    "printType": "BOOK",
    "categories": [
     "Encyclopedias and dictionaries"
    ],
    "maturityRating": "NOT_MATURE",
    "allowAnonLogging": false,
    "contentVersion": "0.1.1.0.preview.0",
    "panelizationSummary": {
     "containsEpubBubbles": false,
     "containsImageBubbles": false
    },
    "imageLinks": {
     "smallThumbnail": "http://books.google.com/books/content?id=SDFIuwEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
     "thumbnail": "http://books.google.com/books/content?id=SDFIuwEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
    },
    "language": "en",
    "previewLink": "http://books.google.it/books?id=SDFIuwEACAAJ&dq=isbn:0716604892&hl=&cd=2&source=gbs_api",
    "infoLink": "http://books.google.it/books?id=SDFIuwEACAAJ&dq=isbn:0716604892&hl=&source=gbs_api",
    "canonicalVolumeLink": "https://books.google.com/books/about/Year_Book.html?hl=&id=SDFIuwEACAAJ"
   }

我需要深入了解:

 "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "0716604892"
     },
     {
      "type": "ISBN_13",
      "identifier": "9780716604891"
     }
    ]

但是我的代码:

String ISBN = "null";
if(volumeInfo.toString().contains("industryIdentifiers")) {
   JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
   if(industryIdentifiers.length() == 1){
       industryIdentifiers = volumeInfo.getJSONObject("industryIdentifiers").getJSONArray("0");
       ISBN = industryIdentifiers.getString(0 ) + ": " + industryIdentifiers.getString(1);
   }
   else if (industryIdentifiers.length() == 2) {
       industryIdentifiers = volumeInfo.getJSONObject("industryIdentifiers").getJSONArray("0");
       ISBN = industryIdentifiers.getString(0 ) + ": " + industryIdentifiers.getString(1);
       industryIdentifiers = volumeInfo.getJSONObject("industryIdentifiers").getJSONArray("1");
       ISBN = ISBN + " - " + industryIdentifiers.getString(0 ) + ": " + industryIdentifiers.getString(1);
        }
      }
   else{ISBN = "null";}

在调试器中产生这个结果(应用程序冻结)。请注意:结果在循环中,您应该只考虑第一行。

E/: Value [{"type":"ISBN_13","identifier":"9788830441392"},{"type":"ISBN_10","identifier":"8830441392"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439719"},{"type":"ISBN_10","identifier":"8830439711"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439696"},{"type":"ISBN_10","identifier":"883043969X"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
    Value [{"type":"ISBN_13","identifier":"9788830447110"},{"type":"ISBN_10","identifier":"8830447110"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830453159"},{"type":"ISBN_10","identifier":"8830453153"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439634"},{"type":"ISBN_10","identifier":"8830439630"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830428454"},{"type":"ISBN_10","identifier":"8830428450"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439658"},{"type":"ISBN_10","identifier":"8830439657"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830449770"},{"type":"ISBN_10","identifier":"8830449776"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830442856"},{"type":"ISBN_10","identifier":"8830442852"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830449480"},{"type":"ISBN_10","identifier":"8830449482"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830441408"},{"type":"ISBN_10","identifier":"8830441406"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
    Value [{"type":"ISBN_13","identifier":"9788830453807"},{"type":"ISBN_10","identifier":"8830453803"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830439610"},{"type":"ISBN_10","identifier":"8830439614"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830441460"},{"type":"ISBN_10","identifier":"8830441465"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830442702"},{"type":"ISBN_10","identifier":"8830442704"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
    Value [{"type":"ISBN_13","identifier":"9788830444997"},{"type":"ISBN_10","identifier":"8830444995"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830448704"},{"type":"ISBN_10","identifier":"8830448702"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830443877"},{"type":"ISBN_10","identifier":"8830443875"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject
E/: Value [{"type":"ISBN_13","identifier":"9788830441415"},{"type":"ISBN_10","identifier":"8830441414"}] at industryIdentifiers of type org.json.JSONArray cannot be converted to JSONObject

我的代码基于这个公认的答案: How to access nested elements of json object using getJSONArray method

如果可能,我不想使用库。

如您所见,我的逻辑是基于我在浏览器上看到的:

【问题讨论】:

  • 为什么先打volumeInfo.getJSONArray("industryIdentifiers"),再打volumeInfo.getJSONObject("industryIdentifiers")?你已经知道它是一个array,那么为什么第二次调用一个object呢?既然您将第一次调用的值分配给了局部变量,为什么还要进行第二次调用?只需使用变量,不要再调用,当然也不要再调用不正确的
  • 我创建了一次行业标识符。我第一次使用它来获取数组维度。在此之后,我再次简单地使用它来获得两个结果。我很困惑,请你给我一个代码吗?

标签: java android json


【解决方案1】:

industryIdentifiers是一个对象数组,所以先调用getJSONArray(String name)获取数组,再调用getJSONObject(int index)获取对象。

你的代码应该是:

JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
if (industryIdentifiers.length() == 1) {
    JSONObject obj = industryIdentifiers.getJSONObject(0);
    ISBN = obj.getString("type") + ": " + obj.getString("identifier");
} else if (industryIdentifiers.length() == 2) {
    JSONObject obj1 = industryIdentifiers.getJSONObject(0);
    JSONObject obj2 = industryIdentifiers.getJSONObject(1);
    ISBN = obj1.getString("type") + ": " + obj1.getString("identifier") + " - "
         + obj2.getString("type") + ": " + obj2.getString("identifier");
}

【讨论】:

  • 这是正确的。和我的方法一样。我只是在 for 循环中迭代和连接 ISBN,因为这将解释存在超过 2 个 ISBN 信息的情况。
【解决方案2】:

这是我将采取的方法:

if(volumeInfo.toString().contains("industryIdentifiers")) {
   JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
if(industryIdentifiers != null && industryIdentifiers.length > 0) {
  for(int i = 0; i < industryIdentifiers.length; i++) {
   JSONObject industryIdentifier = industryIdentifiers.getJSONObject(i);
// Get the ISBN info from the current identifier and concatenate it to your ISBN string
}

} else {
  ISBN = "null";
}

【讨论】:

    【解决方案3】:

    合并两个答案,满足我的需求的完美工作解决方案:

    //ISBN
    String ISBN = "";
         if(volumeInfo.toString().contains("industryIdentifiers")) {
         JSONArray industryIdentifiers = volumeInfo.getJSONArray("industryIdentifiers");
             if (industryIdentifiers.length() > 0) {
                for(int counter = 0; counter < industryIdentifiers.length(); counter++){
                JSONObject obj = industryIdentifiers.getJSONObject(counter);
                ISBN = ISBN + obj.getString("type") + ": " + obj.getString("identifier") + " ";
             }
         }
    }
    else{ISBN = "null";}
    

    【讨论】:

      猜你喜欢
      • 2013-07-29
      • 2016-09-09
      • 1970-01-01
      • 2011-09-05
      • 2019-04-19
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多