JSON.parse语法错误,但JSONLint表示有效

Pengzhi Zhou

我知道有人曾经问过这个问题,但是它并没有解决我的问题,所以我再次在这里提出了问题。我尝试从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");
            }
        ],

我也看不到text您的json中的任何键。所以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");
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章