php json解析对象

时间:2015-07-16 17:40:02

标签: php json parsing

我有一个需要解析的JSON文件。我是php的新手,并试图弄清楚如何正确解析对象。我的问题是:How do I parse the objects of an object that have different names

一个对象的名称为Thresh,列表中的下一个对象的名称为Aatrox,顶级对象的名称为data

这就是JSON的样子。如果我知道对象$champion = $jfo->data->Thresh;的名称,我可以访问该信息,但我不想输入所有冠军的名字。是否有一种简单的方法可以在不知道名称的情况下获取所有单独的对象? Maybe regex?

"data": {
    "Thresh": {
        "id": 412,
        "key": "Thresh",
        "name": "Thresh",
        "title": "the Chain Warden",
        "image": {
            "full": "Thresh.png",
            "sprite": "champion3.png",
            "group": "champion",
            "x": 336,
            "y": 0,
            "w": 48,
            "h": 48
        },

 "Aatrox": {
        "id": 266,
        "key": "Aatrox",
        "name": "Aatrox",
        "title": "the Darkin Blade",
        "image": {
            "full": "Aatrox.png",
            "sprite": "champion0.png",
            "group": "champion",
            "x": 0,
            "y": 0,
            "w": 48,
            "h": 48
        },

1 个答案:

答案 0 :(得分:4)

如果你想通过每个冠军,我建议在PHP中使用foreach循环。您可以这样使用它:

foreach($json->data as $champion)
{
    // Do something
}