2015-10-17 97 views
0

我有一个对象是这样的:PHP json_decode解析问题

[ 
[ 
    "checkthisout.net", 
    "1524621", 
    "1", 
    "Amazing Wines", 
    "", 
    "0", 
    "", 
    " ", 
    " ", 
    "hash", 
    { 
     "i": "http://img.chekthisout.net/xyz.jpg", 
     "l": "//link_to_checkthisout.net", 
     "adc": [] 
    } 
], 
[ 
    "allthebuzz.com", 
    "1482859", 
    "1", 
    "Best Retirement Communities in USA", 
    "", 
    "0", 
    "", 
    " ", 
    " ", 
    "hash", 
    { 
     "i": "http://img.allthbuzz.com/abc.jpg", 
     "l": "//link_to_allthebuzz.com", 
     "adc": [] 
    } 
], 
] 

当我使用json_decode()在此,我得到一个数组的预期。但是,大括号内容被解析为“stdClass”(由调试器报告)。我不确定如何在我的代码中访问这些数据?

+0

您可能希望传递'true'作为'json_decode'的第二个参数:[doc](http://php.net/manual/en/function.json-decode.php) – Federkun

回答

3

true作为json_decode()第二个参数。

这样它将返回一个关联数组而不是一个对象。

或者您可以使用get_object_vars()从对象中获取数据。

+1

传递第二个布尔参数。感谢您的及时回应。 – hvs

3

你必须true值的第二ARG传递给json_decode(),如果你想要得到的结果作为数组

json_decode($json, true); 

如果没有第二ARG传递真实,你会得到stdClass

object(stdClass)#1 (5) { 
    ["a"] => int(1) 
    ["b"] => int(2) 
}