赞
踩
【以下"红粗"代表PHP代码,"黑细"代表在该代码作用下的页面返回。】
总结
对json进行解析:
[ { "id" : "5b554a7df297cb2830ba5be7", "header" : "{ \"endpointKeyHash\" : { \"string\" : \"x8T4iIiKfrjE2YIOQRhWuuG59Fs=\"} , \"applicationToken\" : { \"string\" : \"07530447167571744729\"} , \"headerVersion\" : { \"int\" : 1} , \"timestamp\" : { \"long\" : 1532316285303} , \"logSchemaVersion\" : { \"int\" : 3}}", "event" : "{ \"cashstate\" : 2 , \"latitude\" : 116.31696 , \"longitude\" : 39.99075}" } ]
print_r($J);
Array ( [0] => stdClass Object ( [id] => 5b554a7df297cb2830ba5be7 [header] => { "endpointKeyHash" : { "string" : "x8T4iIiKfrjE2YIOQRhWuuG59Fs="} , "applicationToken" : { "string" : "07530447167571744729"} , "headerVersion" : { "int" : 1} , "timestamp" : { "long" : 1532316285303} , "logSchemaVersion" : { "int" : 3}} [event] => { "cashstate" : 2 , "latitude" : 116.31696 , "longitude" : 39.99075} ) )
print_r($J[0]);
stdClass Object ( [id] => 5b554a7df297cb2830ba5be7 [header] => { "endpointKeyHash" : { "string" : "x8T4iIiKfrjE2YIOQRhWuuG59Fs="} , "applicationToken" : { "string" : "07530447167571744729"} , "headerVersion" : { "int" : 1} , "timestamp" : { "long" : 1532316285303} , "logSchemaVersion" : { "int" : 3}} [event] => { "cashstate" : 2 , "latitude" : 116.31696 , "longitude" : 39.99075} )
print_r($J[0]['event']);
Fatal error: Cannot use object of type stdClass as array in E:\tools\xammp\htdocs\trashbin\index.php on line 32
print_r($J[0]->event);
{ "cashstate" : 2 , "latitude" : 116.31696 , "longitude" : 39.99075}//而且,这是个json,没有变成stdClass Object对象,所以下面无法用$J[0]->event->cashstate
$event=json_decode($J[0]->event);
print_r($event);
stdClass Object ( [cashstate] => 2 [latitude] => 116.31696 [longitude] => 39.99075 ) //这个因为前面是 stdClass Object,所以后面想看cashstate,还得用$event->cashstate
print_r($J);
Array ( [0] => Array ( [id] => 5b554a7df297cb2830ba5be7 [header] => { "endpointKeyHash" : { "string" : "x8T4iIiKfrjE2YIOQRhWuuG59Fs="} , "applicationToken" : { "string" : "07530447167571744729"} , "headerVersion" : { "int" : 1} , "timestamp" : { "long" : 1532316285303} , "logSchemaVersion" : { "int" : 3}} [event] => { "cashstate" : 2 , "latitude" : 116.31696 , "longitude" : 39.99075} ) )
print_r($J[0]['event']);
{ "cashstate" : 2 , "latitude" : 116.31696 , "longitude" : 39.99075}
即,stdClass Object对象的样子为下面,可以用->提取
stdClass Object ( [cashstate] => 2 [latitude] => 116.31696 [longitude] => 39.99075 )
数组的样子为下面,可以用[ ]提取
Array ( [id] => 5b554a7df297cb2830ba5be7 [header] => { "endpointKeyHash" : { "string" : "x8T4iIiKfrjE2YIOQRhWuuG59Fs="} , "applicationToken" : { "string" : "07530447167571744729"} , "headerVersion" : { "int" : 1} , "timestamp" : { "long" : 1532316285303} , "logSchemaVersion" : { "int" : 3}} [event] => { "cashstate" : 2 , "latitude" : 116.31696 , "longitude" : 39.99075} )
json的样子为
{"samplePeriod":5}
或
"{\"samplePeriod\":5}"//这种情况下用json_decode(有没有TRUE结果一样)解析一次为{"samplePeriod":5},再解析一次才为 stdClass Object ( [samplePeriod] => 5 )
第二种情况下的json解析过程为:
print_r($output);
"{\"samplePeriod\":5}"
$J=json_decode($output);
print_r($J);
{"samplePeriod":5}
$JJ=json_decode($J);
print_r($JJ);
stdClass Object ( [samplePeriod] => 5 )
json需要先转换为stdClass Object或Array,才能用->或[ ]
所以调试的时候不妨先把东西打印出来,看是stdClass Object对象、数组、还是json,在进行下一步操作。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。