当前位置:   article > 正文

详解PHP json_decode的TURE_json_decode true

json_decode true

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/456276
推荐阅读
相关标签
  

闽ICP备14008679号