我正在使用以json格式返回数组的API。被接收的JSON的代码是:如何在PHP中访问json数组的特定字段?
Array ([0] => Array ([phone] => 00[_id] => 52e68920acfced017af80234 [name] => John [email] => [email protected] [country] => France) [1] => Array ([phone] => 111222333 [_id] => 52e68acas87fa0987a [name] => James [email] => [email protected] [country] => Germany))
我要访问我的PHP文件单独处理数据的JSON数组的各个领域。例如,访问第一寄存器的名字,我用下面的代码:
$cuerpoRespuesta = ejecutarPOST($URLS['retrieve'], $api_id, $api_key, array()); // This is the API call and I keep the json array in "$cuerpoRespuesta".
$data = json_decode($cuerpoRespuesta, TRUE); //Now I decode json array
echo $data[0]["name"]; //Here I show data
但不显示任何东西,我得到以下错误:
Warning: json_decode() expects parameter 1 to be string, array Given in C:\Retrieve.php on line 65
线65对应于json_decode。我究竟做错了什么?我在这里先向您的帮助表示感谢。
UPDATE:
我已经试过的var_dump($ cuerpoRespuesta)功能,我得到这个:
array (size=2)
0 =>
array (size=5)
'phone' => string '00' (length=9)
'_id' => string '52e68920acfced017af80234' (length=24)
'name' => string 'John' (length=4)
'email' => string '[email protected]' (length=14)
'country' => string 'France' (length=6)
1 =>
array (size=5)
'phone' => string '00' (length=9)
'_id' => string '52e68ac7acfced017af80235' (length=24)
'name' => string 'John' (length=4)
'email' => string '[email protected]' (length=14)
'country' => string 'France' (length=6)
根据API文档是JSON,但是......如果不是,我不知道它是什么:/
的错误是非常不言自明:检查什么是'$ cuerpoRespuesta'通过发出'的var_dump($ cuerpoRespuesta)',并检查什么正在发生。顺便说一句,我建议你不要用本地语言编写代码[以西班牙语为例],它会让你更难理解你在像StackOverflow这样的更广泛的受众中正在做什么。 – moonwave99
它看起来不像JSON,所以你不需要做json_decode。 –
谢谢你的答案。 moonwave99,我不知道如何使用“var_dump ...”你能解释一下吗?非常感谢。 Phil,根据我使用的API文档,返回的数组是JSON格式。 :/ – Monchou2