[英]get a value from json decoded array
我有這個json編碼的字符串
{"allresponses":"{\"id\":\"123456\",\"recipients\":1}"}
而且我只需要獲取ID並將其傳遞給php變量。
這就是我要嘗試的:假設我在變量return中有該字符串,所以:
$return = '{"allresponses":"{\"id\":\"123456\",\"recipients\":1}"}';
$getid = json_decode($return,true);
echo $getid[0]['id'];
這行不通; 我收到致命錯誤。 你能告訴我為什么嗎? 怎么了?
您已經有了json-in-json,這意味着allresponses
的值本身就是一個json字符串,必須分別進行解碼:
$return = '{"allresponses":"{\"id\":\"123456\",\"recipients\":1}"}';
$temp = json_decode($return);
$allresp = $temp['allresponses'];
$temp2 = json_decode($allresp);
echo $temp2['id']; // 123456
請注意,您的$getid[0]
是錯誤的。 您沒有數組。 json純粹是對象( {...}
),因此沒有[0]
索引可訪問。 即使是像var_dump($getid)
類的一些基本調試也可以向您顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.