我有以下 JSON 响应示例(通常是长响应):
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"indent":"true",
"start":"0",
"q":"hi",
"wt":"json",
"rows":"2"}},
"response":{"numFound":69,"start":0,"docs":[
{
"id":335,
"Page":127,
"dartext":" John Said hi ! ",
"Part":1},
{
"id":17124,
"Page":127,
"Tartext":" Mark said hi ",
"Part":10}]
}}
我只想使用字符串类型访问属性问题是属性的名称不是恒定的,所以我不能使用类似的东西:
$obj =json_decode(file_get_contents($data));
$count = $obj->response->numFound;
for($i=0; $i<count($obj->response->docs); $i++){
echo $obj->response->docs[$i]->dartext;
}
因为在另一个对象中它不是 dartext 它是 Tartext。
如何通过索引而不是名称访问第三个属性?