- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道之前有人问过这个问题,但是没有解决我的问题,所以我又在这里问了。我尝试从 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/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!