创建php脚本来解析这个json

时间:2013-03-17 23:39:15

标签: php json parsing

我想在php中解析这个json数据,任何人都可以在脚本中帮助我。

以下代码

  

{members“:[           {               “会员”:{                   “id”:464258,                   “display_name”:“test1”,                   “mitch_rank_index”:6.64,                   “距离”:0.56009166639932,                   “unread_messages_from”:0,                   “探险家”:虚假,                   “在线”:是的,                   “最爱”:假,                   “粉丝”:假的,                   “thumbnail_url”:“链接”,                   “profile_photo_url”:“链接”               }           },           {               “会员”:{                   “id”:1009345,                   “display_name”:“Test2”,                   “mitch_rank_index”:6.32,                   “距离”:0.583112841628013,                   “unread_messages_from”:0,                   “探险家”:虚假,                   “在线”:错误,                   “最爱”:假,                   “粉丝”:假的,                   “thumbnail_url”:“链接”,                   “profile_photo_url”:“链接”               }           },           {               “会员”:{                   “id”:1052568,                   “display_name”:null,                   “mitch_rank_index”:5.699999999999999,                   “距离”:0.597684462292014,                   “unread_messages_from”:0,                   “探险家”:虚假,                   “在线”:是的,                   “最爱”:假,                   “粉丝”:假的,                   “thumbnail_url”:“链接”,                   “profile_photo_url”:“链接”               }           }       ]

}

2 个答案:

答案 0 :(得分:2)

尝试使用:

json_decode()

您可以在此处详细了解此功能:http://php.net/manual/en/function.json-decode.php

答案 1 :(得分:1)

这是从它生成的有效json和数组。像处理PHP数组一样对待它。

<?php

$json = <<<JSON
{
    "members": [
        {
            "member": {
                "id": 464258,
                "display_name": "test1",
                "mitch_rank_index": 6.64,
                "distance": 0.56009166639932,
                "unread_messages_from": 0,
                "explorer": false,
                "online": true,
                "favourite": false,
                "fan": false,
                "thumbnail_url": "link",
                "profile_photo_url": "link"
            }
        },
        {
            "member": {
                "id": 1009345,
                "display_name": "Test2",
                "mitch_rank_index": 6.32,
                "distance": 0.583112841628013,
                "unread_messages_from": 0,
                "explorer": false,
                "online": false,
                "favourite": false,
                "fan": false,
                "thumbnail_url": "link",
                "profile_photo_url": "Link"
            }
        },
        {
            "member": {
                "id": 1052568,
                "display_name": null,
                "mitch_rank_index": 5.699999999999999,
                "distance": 0.597684462292014,
                "unread_messages_from": 0,
                "explorer": false,
                "online": true,
                "favourite": false,
                "fan": false,
                "thumbnail_url": "link",
                "profile_photo_url": "link"
            }
        }
    ]
}
JSON;

$json = json_decode($json,true);

print("<pre>");
print_r($json);
print("</pre>");

?>

<强>更新

要显示JSON中的所有ID,您可以从以下代码中获取示例

// $ids will contain array of all ID that ara available in JSON
foreach ($json['members'] as $members) $ids[] = $members['member']['id'];

// you can use $ids array from now
// following code just shows how array can look like in php
print("<pre>");
print_r($ids);
print("</pre>");

// use foreach to go through all IDs and do something for each of them
// this code simply goes through all IDs and prints each of them on screen
// again, this is a way to manipulate through all IDS from your JSON
foreach ($ids as $id)
{
    // you can use $id here in this loop
    print("id: ".$id."<br />");
}