【问题标题】:Accessing the inner array's values in a JSONArray在 JSONArray 中访问内部数组的值
【发布时间】:2019-11-12 19:38:32
【问题描述】:

我正在使用 IntelliJ 和 Gooks Books API,并且在使用简单 json 的同时使用 Java 进行编码。我正在编写一个程序,用户可以在终端中键入书名,API 返回 5 本书和每本书的信息。 API 返回很多关于书籍的信息,但我只希望它返回书名、作者和出版商。我是使用 APIs 和 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”数组中调用信息不同。如何从“items”数组中仅针对“title”和“publisher”键的值检索信息?

【问题讨论】:

    标签: java arrays api intellij-idea json-simple


    【解决方案1】:

    我可以在上面的代码中看到你的努力,但是 authorvolumeInfo 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");
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 2013-05-03
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多