在尝试解析JSON数组时获取Undefined属性:stdClass :: $ insertion

时间:2013-07-10 20:18:04

标签: php json parsing

我有这个json,我尝试提取插入顺序数组

{
    "response" : {
        "status" : "OK",
        "count" : 1,
        "insertion-orders" : [{
                "id" : 5,
                "name" : "First Choice",
                "code" : null,
                "state" : "active",
                "line_items" : [{
                        "id" :352,
                        "timezone" : "Europe/London"
                    }, {
                        "id" :358,
                        "timezone" : "Europe/London"
                    }
                ],
                "labels" : null,
                "safety_pacing" : true
            }
        ]
    }
}

我正在做的是TheJson是json字符串:

$Json = json_decode($TheJson);
$JsonResponse = $Json->response;
$OrdersArray = $JsonResponse->insertion-orders;

我得到的错误是:

Notice: Undefined property: stdClass::$insertion in /home/foo/a/foo.com/user/htdocs/FunctionManager.php on line 16

Notice: Use of undefined constant orders - assumed 'orders' in /home/foo/a/foo.com/user/htdocs/FunctionManager.php on line 16

第16行是:

 $OrdersArray = $JsonResponse->insertion-orders;

我只是没有得到它,它的有效json

1 个答案:

答案 0 :(得分:2)

$JsonResponse->insertion-orders;

被解析为

$JSonResponse->insert MINUS orders

您不能使用箭头表示法在对象属性名称中使用-。它必须是

$JsonResponse->{'insertion-orders'}

代替。

至于你关于它是有效JSON的评论,它在Javascript中也不起作用:

json.insertion-orders 

也会被视为json.insertion MINUS orders