php json_decode解析问题

时间:2015-10-17 17:28:50

标签: php json

我有一个这样的对象:

[
[
    "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”(由调试器报告)。我不确定如何在我的代码中访问这些数据?

2 个答案:

答案 0 :(得分:3)

如果您希望将结果作为数组

,则必须将true值的第二个arg传递给 this
json_decode($json, true);

如果你没有为第二个arg传递真实,那么你会得到stdClass

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

答案 1 :(得分:3)

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

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

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