0

我有这个 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

4

1 回答 1

2
$JsonResponse->insertion-orders;

被解析为

$JSonResponse->insert MINUS orders

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

$JsonResponse->{'insertion-orders'}

反而。

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

json.insertion-orders 

也将被视为json.insertion MINUS orders

于 2013-07-10T20:20:17.167 回答