在PHP中解析JSON错误

时间:2013-06-27 23:22:29

标签: php json parsing

我想解析这个JSON:

{
   "date":[
      "dia_29_06_13"
   ],
   "jsonList":{
      "__type":"File",
      "name":"d138af3c-4da0-4ab9-b3ff-0ac09a352fd8-lista.json",
   "localName":[
      "Patropi"
   ],
   "pspReference":"8513723597141689",
   "qrcode":"KWX35ERoNJ",
   "status":"true",
   "valor":127,
   "createdAt":"2013-06-27T19:01:46.830Z",
   "updatedAt":"2013-06-27T19:01:57.830Z",
   "objectId":"fcXMwYaDMQ",
   "ACL":{
      "*":{
         "write":true,
         "read":true
      },
      "KWX35ERoNJ":{
         "write":true,
         "read":true
      }
   }
}

但它总是给我这个错误:

date: 0: => dia_29_06_13
Catchable fatal error: Object of class stdClass could not be converted to string in /home/storage/a/1c/6e/semhora/public_html/payment/temp3.php on line 42

我正在使用此代码:

$response = curl_exec($rest);

        $jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($response)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key: => $val";

    }
}

2 个答案:

答案 0 :(得分:1)

只要您希望嵌套项是数组 - 您需要将其解析为数组:

json_decode($response, true)

当第二个参数设置为true时,JSON字符串将被解析为数组,而不是stdClass

答案 1 :(得分:0)

您在上面发布的JSON无效。

JSON Lint上查看。

你打开一个大括号而不是你关闭。