简体   繁体   中英

getting Undefined property: stdClass::$insertion when try to parse JSON array

i have this json on which i try to extract the insertion-orders array

{
    "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
            }
        ]
    }
}

what im doing is TheJson is the json string:

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

the error im getting is :

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

line 16 is :

 $OrdersArray = $JsonResponse->insertion-orders;

i just don't get it , its valid json

$JsonResponse->insertion-orders;

is parsed as

$JSonResponse->insert MINUS orders

You can't use - in an object attribute name using the arrow notation. It'll have to be

$JsonResponse->{'insertion-orders'}

instead.

As for your comment about it being valid JSON, it wouldn't have worked in Javascript either:

json.insertion-orders 

will also be seen as json.insertion MINUS orders .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM