[英]Accessing the inner array's values in a JSONArray
I am using IntelliJ and the Gooks Books API and I'm coding in Java while using json simple.我正在使用 IntelliJ 和 Gooks Books API 并且我在 Java 中进行编码,同时使用 json 简单。 I am writing a program where the user can type a book title in terminal and the API returns 5 books and information on each of those books.
我正在编写一个程序,用户可以在终端中键入书名,API 返回 5 本书和每本书的信息。 The API returns a lot of information on the books but I only want it to return the book title, author, and publisher.
API 返回很多关于书籍的信息,但我只希望它返回书名、作者和出版商。 I'm new to using APis and JSON so I don't know if my errors can be from syntax or logic errors.
我是使用 APIs 和 JSON 的新手,所以我不知道我的错误是否来自语法或逻辑错误。
For example, I type in Harry Potter and this is part of what the API returns例如,我输入 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"
],
I've read that one of the ways to retrieve specific information is to create a JSONArray and make the information from the API be JSONObjects.我读过,检索特定信息的方法之一是创建一个 JSONArray 并使来自 API 的信息成为 JSONObjects。 I was successfully able to parse the data so each JSONObject is one book and all it's information, but I don't know how to ONLY print out the specific information I want.
我成功地解析了数据,所以每个 JSONObject 都是一本书和所有的信息,但我不知道如何只打印出我想要的特定信息。
This is some of the code I have.这是我的一些代码。 The code from TODO onward is where I'm having trouble
从 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
}
From my understanding, everything is under the "items" array, so the "authors" array is an array inside another array.据我了解,一切都在“项目”数组下,所以“作者”数组是另一个数组中的一个数组。 How do I access the information in the "authors" array?
如何访问“作者”数组中的信息?
I also need the title and publisher of the book and I know those aren't arrays so it's unlike calling for the information from the "authors" array.我还需要这本书的标题和出版商,我知道这些不是 arrays,所以它不像从“作者”数组中调用信息。 How do I retrieve the information from the "items" array just for the values for the keys "title" and "publisher"?
我如何从“items”数组中检索信息,仅用于键“title”和“publisher”的值?
I can see your effort in the above code, But author
is inside of volumeInfo
JsonObject我可以在上面的代码中看到你的努力,但是
author
在volumeInfo
JsonObject 里面
"volumeInfo": {
"title": "Harry Potter and the Cursed Child",
"subtitle": "Parts one and two",
"authors": [
"Jack Thorne",
"J. K. Rowling",
"John Tiffany"
],
So in the for loop first you need to get volumeInfo
as JSONObject
and then get author
as JSONArray
因此,在 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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.