Java 访问内部阵列';JSONArray中的s值
我在使用IntelliJ和Gooks Books API,我在使用json simple的同时用Java编码。我正在编写一个程序,用户可以在终端中键入一本书的标题,API将返回5本书以及每本书的相关信息。API返回了很多关于书籍的信息,但我只希望它返回书名、作者和出版商。我不熟悉使用API和JSON,所以我不知道我的错误是来自语法还是逻辑错误 例如,我输入Harry Potter,这是API返回的内容的一部分Java 访问内部阵列';JSONArray中的s值,java,arrays,api,intellij-idea,json-simple,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":
{
"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中的信息成为JSONObject。我成功地解析了数据,因此每个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
}
String inline=”“//获取JSON数据并使其成为字符串
Scanner sc=新的扫描仪(url.openStream())//读取JSON数据
while(sc.hasNext())
{
inline+=sc.nextLine();
}
sc.close();
JSONParser parse=新的JSONParser();
JSONObject jObj=(JSONObject)parse.parse(内联);
JSONArray theJArray=(JSONArray)jObj.get(“items”);
for(int i=0;i
据我所知,所有内容都在“items”数组下,因此“authors”数组是另一个数组中的数组。如何访问“作者”数组中的信息
我还需要书名和出版商,我知道这些不是数组,所以它不像从“authors”数组中调用信息。如何从“items”数组中检索仅针对键“title”和“publisher”的值的信息?我可以在上面的代码中看到您的努力,但是
作者
在卷信息
JsonObject中
"volumeInfo": {
"title": "Harry Potter and the Cursed Child",
"subtitle": "Parts one and two",
"authors": [
"Jack Thorne",
"J. K. Rowling",
"John Tiffany"
],
因此,在for循环中,首先需要获取volumeInfo
asJSONObject
,然后获取author
asJSONArray
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");
}
for(int i=0;i
我可以在上面的代码中看到您的工作,但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
asJSONObject
,然后获取author
asJSONArray
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");
}
for(int i=0;i