java - 访问 JSONArray 中内部数组的值

标签 java arrays api intellij-idea json-simple

我正在使用 IntelliJ 和 Gooks Books API,并且在使用 json simple 的同时使用 Java 进行编码。我正在编写一个程序,用户可以在终端中输入书名,API 返回 5 本书以及每本书的信息。该 API 返回大量有关书籍的信息,但我只希望它返回书名、作者和出版商。我刚开始使用 API 和 JSON,所以我不知道我的错误是否来自语法或逻辑错误。

例如,我输入 Harry Potter,这是 API 返回内容的一部分

{
 "kind": "books#volumes",
 "totalItems": 1832,
 "items": [
  {
   "kind": "books#volume",
   "id": "cvRFvgAACAAJ",
   "etag": "mfbVxWEkveo",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/cvRFvgAACAAJ",
   "volumeInfo": {
    "title": "Harry Potter and the Cursed Child",
    "subtitle": "Parts one and two",
    "authors": [
     "Jack Thorne",
     "J. K. Rowling",
     "John Tiffany"
    ],
    "publisher": "THORNDIKE Press",
    "publishedDate": "2016",
    "description": "\"Thorndike Press large print the literacy bridge.\"",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "1410496201"
     },
     {
      "type": "ISBN_13",
      "identifier": "9781410496201"
     }
    ],
    "readingModes": {
     "text": false,
     "image": false
    },
    "pageCount": 407,
    "printType": "BOOK",
    "categories": [
     "Good and evil"
    ],

我读到检索特定信息的方法之一是创建 JSONArray 并使来自 API 的信息成为 JSONObjects。我成功地解析了数据,因此每个 JSONObject 都是一本书及其所有信息,但我不知道如何只打印出我想要的特定信息。

这是我的一些代码。从 TODO 开始的代码是我遇到问题的地方

String inline = ""; //gets the JSON data and makes it a String
    Scanner sc = new Scanner(url.openStream()); //reads JSON data
    while (sc.hasNext())
    {
      inline += sc.nextLine();
    }
    sc.close();

    JSONParser parse = new JSONParser();
    JSONObject jObj = (JSONObject)parse.parse(inline);
    JSONArray theJArray = (JSONArray)jObj.get("items");

    for (int i = 0; i < theJArray.size(); i++)
    {
      JSONObject secondJObj = (JSONObject)theJArray.get(i);
      System.out.println("The secondJobj: " + secondJObj); //one object is one book and its info

      JSONArray specificArr = (JSONArray)secondJObj.get("author"); //TODO
      System.out.println("The specificArr is: " + specificArr); //returns null
      System.out.println("The specific section: " + secondJObj.get("author")); //returns null
    }

根据我的理解,所有内容都在“items”数组下,因此“authors”数组是另一个数组中的一个数组。如何访问“authors”数组中的信息?

我还需要书的标题和出版商,我知道这些不是数组,所以这与从“authors”数组中调用信息不同。如何从“items”数组中检索“title”和“publisher”键值的信息?

最佳答案

我可以在上面的代码中看到你的努力,但是author位于volumeInfo JsonObject内部

"volumeInfo": {
"title": "Harry Potter and the Cursed Child",
"subtitle": "Parts one and two",
"authors": [
 "Jack Thorne",
 "J. K. Rowling",
 "John Tiffany"
],

因此,在 for 循环中,首先需要获取 volumeInfo 作为 JSONObject,然后获取 author 作为 JSONArray

for (int i = 0; i < theJArray.size(); i++)
{
  JSONObject secondJObj = (JSONObject)theJArray.get(i);
  System.out.println("The secondJobj: " + secondJObj); //one object is one book and its info

  JSONObject volInfo = (JSONObject)secondJObj.get("volumeInfo"); //TODO
    // then get author
   JSONArray specificArr = (JSONArray)volInfo.get("author");
}

关于java - 访问 JSONArray 中内部数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58825572/

相关文章:

c - 声明大数组时出现堆栈溢出异常

java - 使用 Appium 读取应用程序的 api 调用

java - JScrollbar 拇指/旋钮较小时未正确绘制

Java:有效地将长整型数组转换为字节数组

java - 如何以编程方式禁用 Java LDAP JNDI LDAP API 中的证书主机名验证?

c - 尝试为动态分配的二维数组赋值时程序崩溃

arrays - Bash:如何计算数组中的唯一值并检索具有最多重复项的值

javascript - 如何将 "hole"添加到圆形多边形(Google Maps API V3)

javascript - 我想制作一张世界地图,人们点击我的网站就像 Spark 一样。最好的方法是什么?

java - 在selenium JAVA中提取文本br标签