使用php从json获取数据
请查找接收到的json iam:使用php从json获取数据,php,json,Php,Json,请查找接收到的json iam: { "batchcomplete":"", "continue":{ "sroffset":10, "continue":"-||" }, "query":{ "searchinfo":{ "totalhits":1937 }, "search":[ { "ns":0, "title":"India", "pageid":15557,
{
"batchcomplete":"",
"continue":{
"sroffset":10,
"continue":"-||"
},
"query":{
"searchinfo":{
"totalhits":1937
},
"search":[
{
"ns":0,
"title":"India",
"pageid":15557,
"size":213703,
"wordcount":30816,
"snippet":"<span class=\"searchmatch\">India</span> (Hindi: \u092d\u093e\u0930\u0924 or Bharat) is the largest country in the Indian subcontinent and shares borders with Pakistan to the northwest, China and Nepal to",
"timestamp":"2018-05-27T12:16:15Z"
},
{
"ns":0,
"title":"Southern India",
"pageid":33554,
"size":20350,
"wordcount":2534,
"snippet":"South <span class=\"searchmatch\">India</span> includes five major states in peninsular <span class=\"searchmatch\">India</span> and the two island groups of Lakshadweep in the Arabian Sea on the West Coast of <span class=\"searchmatch\">India</span> and Andaman",
"timestamp":"2018-05-13T14:30:50Z"
}}
但是我在这行中遇到了一个错误..echo$country->searchinfo;错误表明未定义的属性:stdClass::$searchinfo
我试图只检索代码段变量(第一个)
有人能帮我吗。1.
$data->queryfo
需要$data->query
2.您必须这样更改foreach()
:-
foreach ($data->query->search as $country)
{
echo $country->snippet;echo PHP_EOL;
}
注意:-我已按您所说的更改了代码
我正在尝试接收代码段而不是totalhits
您确定它是$data->queryfo
而不是$data->query
?抱歉。这是一个打字错误。它是“查询”。不过,这是您实际收到的JSON吗?它包含无法解码的语法错误。Iam从该链接接收json数据@三菊很高兴帮助你:):)
foreach ($data->query->search as $country)
{
echo $country->snippet;echo PHP_EOL;
}