如果之前有人問過這個問題,我很抱歉(我已經完全搜索過了,但仍然無法弄清楚)。我正在嘗試從 google 知識圖譜 API 訪問嵌套元素。這是 JSON 的樣子
{
"@context": {
"goog": "http://schema.googleapis.com/",
"kg": "http://g.co/kg",
"EntitySearchResult": "goog:EntitySearchResult",
"resultScore": "goog:resultScore",
"@vocab": "http://schema.org/",
"detailedDescription": "goog:detailedDescription"
},
"@type": "ItemList",
"itemListElement": [
{
"result": {
"name": "Samoyed",
"description": "Dog breed",
"@type": [
"Thing"
],
"@id": "kg:/m/017lg8",
"detailedDescription": {
"url": "https://en.wikipedia.org/wiki/Samoyed_dog",
"articleBody": "The Samoyed is a breed of medium-sized herding dogs with thick, white, double-layer coats. They are a spitz-type dog which takes its name from the Samoyedic peoples of Siberia.",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
}
},
"@type": "EntitySearchResult",
"resultScore": 7775.93505859375
}
]
}
我需要訪問 articleBody 元素。這是到目前為止我在 dart 中得到的內容:
Future<Dog> getWikiInfo() async {
var query = "Samoyed";
var API_KEY = "*************";
final apiURL =
"https://kgsearch.googleapis.com/v1/entities:search?query=$query&key=$API_KEY&limit=1&indent=True";
final response = await http.get(Uri.parse(apiURL));
if (response.statusCode == 200) {
for (var element in json.decode(response.body)['itemListElement']) {
for (var subelement
in json.decode(response.body)['detailedDescription']) {
print(subelement['articleBody']);
}
}
}
我一直在嘗試不同的迭代來嘗試回傳該元素。這是我的 Dog Class 的樣子。
class Dog {
final String link;
final String breed;
final String info;
Dog({this.link, this.breed, this.info});
factory Dog.fromJson(Map<String, dynamic> json) {
return Dog(
breed: json['message'],
link: json['message'],
info: json['articleBody']);
}
}
uj5u.com熱心網友回復:
根據您的邏輯嘗試此序列代碼
json.decode(response.body)["itemListElement"][0]["result"]["detailedDescription"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360802.html
上一篇:如何檢查FlutterFirestore中是否存在用戶檔案?
下一篇:如何讓定時器一直顯示
