详解PHP json_decode的TURE

【以下"红粗"代表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}" } ]

详解PHP json_decode的TURE_第1张图片

$J=json_decode($output);//第一层以数组形式出来(若上面是[{ },{ }],则有$J[0]、$J[1] ),但数组里面存的对象stdClass //Object类型$J[0]->event可以,$J[0]['event']不可以

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

$J=json_decode($output,TRUE); //第一层以数组形式出来,且数组里面存的对象还是数组类型,即形成了两层数组

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,在进行下一步操作。

你可能感兴趣的:(详解PHP json_decode的TURE)