Javascript 如何在php中仅从知识图api中提取文章正文?
我试图在php中仅从json ld获取文章正文,但我不明白如何获取 我不太熟悉从php对json进行编码和解码,所以似乎什么都不管用Javascript 如何在php中仅从知识图api中提取文章正文?,javascript,php,json,google-knowledge-graph,Javascript,Php,Json,Google Knowledge Graph,我试图在php中仅从json ld获取文章正文,但我不明白如何获取 我不太熟悉从php对json进行编码和解码,所以似乎什么都不管用 "@context": { "@vocab": "http://schema.org/", "goog": "http://schema.googleapis.com/", "resultScore": "goog:resultScore", "detailedDescription": "goog:detailedDescri
"@context": {
"@vocab": "http://schema.org/",
"goog": "http://schema.googleapis.com/",
"resultScore": "goog:resultScore",
"detailedDescription": "goog:detailedDescription",
"EntitySearchResult": "goog:EntitySearchResult",
"kg": "http://g.co/kg"
},
"@type": "ItemList",
"itemListElement": [
{
"@type": "EntitySearchResult",
"result": {
"@id": "kg:/m/0dl567",
"name": "Taylor Swift",
"@type": [
"Thing",
"Person"
],
"description": "Singer-songwriter",
"image": {
"contentUrl": "https://t1.gstatic.com/images?q=tbn:ANd9GcQmVDAhjhWnN2OWys2ZMO3PGAhupp5tN2LwF_BJmiHgi19hf8Ku",
"url": "https://en.wikipedia.org/wiki/Taylor_Swift",
"license": "http://creativecommons.org/licenses/by-sa/2.0"
},
"detailedDescription": {
"articleBody": "Taylor Alison Swift is an American singer-songwriter and actress. Raised in Wyomissing, Pennsylvania, she moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music. ",
"url": "http://en.wikipedia.org/wiki/Taylor_Swift",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
},
"url": "http://taylorswift.com/"
},
"resultScore": 896.576599
}
]
}
我只需要这篇文章的主体是“泰勒·艾莉森·斯威夫特是一位美国歌手、作曲家和女演员……”。如何实现这一点?您必须先解码此字符串,然后它才能从数组中提取您的需要。例如
$j = '{"@context": {"@vocab": "http://schema.org/", "goog": "http://schema.googleapis.com/", "resultScore": "goog:resultScore" }}';
$arr = json_decode($j, true);
echo $arr['@context']['goog'];
对于articleBody
,它应该是:
$arr['itemListElement'][0]['result']['detailedDescription']['articleBody']
你必须解码这个字符串,然后它只是从数组中提取你的需要。例如
$j = '{"@context": {"@vocab": "http://schema.org/", "goog": "http://schema.googleapis.com/", "resultScore": "goog:resultScore" }}';
$arr = json_decode($j, true);
echo $arr['@context']['goog'];
对于articleBody
,它应该是:
$arr['itemListElement'][0]['result']['detailedDescription']['articleBody']
它说“未定义的索引:@itemListElement”,这是我修改过的代码:但现在它显示“未定义的索引:结果”。问题可能是由什么引起的?请尝试
['itemListElement'][0]['result']['detailedDescription']['articleBody']
它说的是“未定义的索引:@itemListElement”这是我修改过的代码:但现在它显示的是“未定义的索引:结果”。问题可能是由什么引起的?请尝试['itemListElement'][0]['result']['detailedDescription']['articleBody']