gpt4 book ai didi

android - JSON.parse 语法错误,但 JSONLint 表示它是有效的

转载 作者:行者123 更新时间:2023-11-29 00:25:19 25 4
gpt4 key购买 nike

我知道之前有人问过这个问题,但是没有解决我的问题,所以我又在这里问了。我尝试从 https://openlibrary.org/dev/docs/api/books 下载 json 数据,我的代码是这样的:

public void looupBook() {
EditText isbntext = (EditText) findViewById(R.id.editText4);
String isbn = isbntext.getText().toString();

if (isbn.equals("")) {
showToast(this, "ISBN can not be null when using Lookup function",
3);
} else {
String readBookFeed = readBookFeed(isbn);
//String formatReadBookFeed = formatJsonString(readBookFeed);
try {
JSONArray jsonArray = new JSONArray(readBookFeed);//it failed here
Log.i(CreateBookActivity.class.getName(), "Number of entries "
+ jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(CreateBookActivity.class.getName(),
jsonObject.getString("text"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

public String readBookFeed(String isbn) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String request = "https://openlibrary.org/api/books?bibkeys=ISBN:"
+ "0451526538";
request = request + "&jscmd=data&format=json";

HttpGet httpGet = new HttpGet(request);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(CreateBookActivity.class.toString(),
"Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}

rebookfeed 的响应是如下字符串:

{
"ISBN:0451526538": {
"publishers": [
{
"name": "Signet Classic"
}
],
"pagination": "xxi, 216 p. ;",
"identifiers": {
"lccn": [
"96072233"
],
"openlibrary": [
"OL1017798M"
],
"isbn_10": [
"0451526538"
],
"oclc": [
"36792831"
],
"goodreads": [
"1929684"
],
"project_gutenberg": [
"74"
],
"librarything": [
"2236"
]
},
"classifications": {
"dewey_decimal_class": [
"813/.4"
],
"lc_classifications": [
"PS1306 .A1 1997"
]
},
"title": "The adventures of Tom Sawyer",
"url": "https://openlibrary.org/books/OL1017798M/The_adventures_of_Tom_Sawyer",
"notes": "Includes bibliographical references (p. 213-216).",
"number_of_pages": 216,
"cover": {
"small": "https://covers.openlibrary.org/b/id/295577-S.jpg",
"large": "https://covers.openlibrary.org/b/id/295577-L.jpg",
"medium": "https://covers.openlibrary.org/b/id/295577-M.jpg"
},
"subject_places": [
{
"url": "https://openlibrary.org/subjects/place:missouri",
"name": "Missouri"
},
{
"url": "https://openlibrary.org/subjects/place:mississippi_river",
"name": "Mississippi River"
},
{
"url": "https://openlibrary.org/subjects/place:mississippi_river_valley",
"name": "Mississippi River Valley"
},
{
"url": "https://openlibrary.org/subjects/place:misuri",
"name": "Misuri"
},
{
"url": "https://openlibrary.org/subjects/place:southern_states",
"name": "Southern States"
},
{
"url": "https://openlibrary.org/subjects/place:mississippi",
"name": "Mississippi"
},
{
"url": "https://openlibrary.org/subjects/place:misisipí_(río)",
"name": "Misisipí (Río)"
},
{
"url": "https://openlibrary.org/subjects/place:united_states",
"name": "United States"
},
{
"url": "https://openlibrary.org/subjects/place:río_misisipi",
"name": "Río Misisipi"
}
],
"subjects": [
{
"url": "https://openlibrary.org/subjects/fiction",
"name": "Fiction"
},
{
"url": "https://openlibrary.org/subjects/tom_sawyer_(fictitious_character)",
"name": "Tom Sawyer (Fictitious character)"
},
{
"url": "https://openlibrary.org/subjects/boys",
"name": "Boys"
},
{
"url": "https://openlibrary.org/subjects/juvenile_fiction",
"name": "Juvenile fiction"
},
{
"url": "https://openlibrary.org/subjects/runaway_children",
"name": "Runaway children"
},
{
"url": "https://openlibrary.org/subjects/child_witnesses",
"name": "Child witnesses"
},
{
"url": "https://openlibrary.org/subjects/male_friendship",
"name": "Male friendship"
},
{
"url": "https://openlibrary.org/subjects/history",
"name": "History"
},
{
"url": "https://openlibrary.org/subjects/adventure_and_adventurers",
"name": "Adventure and adventurers"
},
{
"url": "https://openlibrary.org/subjects/huckleberry_finn_(fictitious_character)",
"name": "Huckleberry Finn (Fictitious character)"
},
{
"url": "https://openlibrary.org/subjects/ficción_juvenil",
"name": "Ficción juvenil"
},
{
"url": "https://openlibrary.org/subjects/fugitive_slaves",
"name": "Fugitive slaves"
},
{
"url": "https://openlibrary.org/subjects/race_relations",
"name": "Race relations"
},
{
"url": "https://openlibrary.org/subjects/history_and_criticism",
"name": "History and criticism"
},
{
"url": "https://openlibrary.org/subjects/american_adventure_stories",
"name": "American Adventure stories"
},
{
"url": "https://openlibrary.org/subjects/mississippi_river",
"name": "Mississippi River"
},
{
"url": "https://openlibrary.org/subjects/children's_stories,_american",
"name": "Children's stories, American"
},
{
"url": "https://openlibrary.org/subjects/social_life_and_customs",
"name": "Social life and customs"
},
{
"url": "https://openlibrary.org/subjects/adventure_stories",
"name": "Adventure stories"
},
{
"url": "https://openlibrary.org/subjects/facsimiles",
"name": "Facsimiles"
},
{
"url": "https://openlibrary.org/subjects/missouri",
"name": "Missouri"
},
{
"url": "https://openlibrary.org/subjects/sawyer,_tom_(personaje_literario)",
"name": "Sawyer, Tom (Personaje literario)"
},
{
"url": "https://openlibrary.org/subjects/cartoons_and_comics",
"name": "Cartoons and comics"
},
{
"url": "https://openlibrary.org/subjects/spanish_language_books",
"name": "Spanish language books"
},
{
"url": "https://openlibrary.org/subjects/readers",
"name": "Readers"
},
{
"url": "https://openlibrary.org/subjects/relatos_de_aventura",
"name": "Relatos de aventura"
},
{
"url": "https://openlibrary.org/subjects/specimens",
"name": "Specimens"
},
{
"url": "https://openlibrary.org/subjects/dummies_(bookselling)",
"name": "Dummies (Bookselling)"
},
{
"url": "https://openlibrary.org/subjects/manuscripts",
"name": "Manuscripts"
},
{
"url": "https://openlibrary.org/subjects/readers_for_new_literates",
"name": "Readers for new literates"
},
{
"url": "https://openlibrary.org/subjects/high_interest-low_vocabulary_books",
"name": "High interest-low vocabulary books"
},
{
"url": "https://openlibrary.org/subjects/children's_stories",
"name": "Children's stories"
},
{
"url": "https://openlibrary.org/subjects/muchachos",
"name": "Muchachos"
},
{
"url": "https://openlibrary.org/subjects/niños",
"name": "Niños"
},
{
"url": "https://openlibrary.org/subjects/translations_into_russian",
"name": "Translations into Russian"
},
{
"url": "https://openlibrary.org/subjects/translations_into_polish",
"name": "Translations into Polish"
},
{
"url": "https://openlibrary.org/subjects/spanish_language",
"name": "Spanish language"
},
{
"url": "https://openlibrary.org/subjects/translations_into_czech",
"name": "Translations into Czech"
},
{
"url": "https://openlibrary.org/subjects/american_manuscripts",
"name": "American Manuscripts"
},
{
"url": "https://openlibrary.org/subjects/historical_fiction",
"name": "Historical fiction"
},
{
"url": "https://openlibrary.org/subjects/accessible_book",
"name": "Accessible book"
},
{
"url": "https://openlibrary.org/subjects/protected_daisy",
"name": "Protected DAISY"
},
{
"url": "https://openlibrary.org/subjects/in_library",
"name": "In library"
},
{
"url": "https://openlibrary.org/subjects/lending_library",
"name": "Lending library"
},
{
"url": "https://openlibrary.org/subjects/juvenile_literature",
"name": "Juvenile literature"
},
{
"url": "https://openlibrary.org/subjects/large_type_books",
"name": "Large type books"
},
{
"url": "https://openlibrary.org/subjects/adventure_and_adventures",
"name": "Adventure and adventures"
}
],
"subject_people": [
{
"url": "https://openlibrary.org/subjects/person:mark_twain_(1835-1910)",
"name": "Mark Twain (1835-1910)"
}
],
"key": "/books/OL1017798M",
"authors": [
{
"url": "https://openlibrary.org/authors/OL18319A/Mark_Twain",
"name": "Mark Twain"
}
],
"publish_date": "1997",
"by_statement": "Mark Twain ; with an introduction by Robert S. Tilton.",
"publish_places": [
{
"name": "New York"
}
],
"subject_times": [
{
"url": "https://openlibrary.org/subjects/time:19th_century",
"name": "19th century"
}
]
}
}

我已经在 http://jsonlint.org/ 中测试了这个字符串,它说这是一个有效的 json 数据,但是当我使用

解析它时
 JSONArray jsonArray = new JSONArray(readBookFeed); 

发生异常。 jsontype 不匹配。

我不知道哪里出了问题,任何人都可以提出一些建议吗?

谢谢

最佳答案

返回的 json 不是 JSONArray,而是 JSONObject

改变这个

JSONArray jsonArray = new JSONArray(readBookFeed);

JSONObject jsonObject = new JSONObject(readBookFeed);

{代表一个json对象节点

[代表一个json数组节点

JSON

{ // represetns json object node 
"ISBN:0451526538": { // json object
"publishers": [ // json array publishers
{ // json object
"name": "Signet Classic" // string jsonObject.getString("name");
}
],

此外,我在您的 json 中没有看到 text 键。所以 jsonObject.getString("text")) 不会工作

编辑:

 JSONObject jsonObject = new JSONObject(readBookFeed);
JSONObject jb = jsonobject.getJSONObject("ISBN:0451526538");
JSONArray jarr - jb.getJSONArray("publishers");
for(int i=0;i<jarr.length();i++)
{
JSONObject jb1 =(JSONObject)jarr.get(i);
String name = jb1.getString("name");
}

关于android - JSON.parse 语法错误,但 JSONLint 表示它是有效的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20031980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com