-2

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

下面的代码

{成员”:[{“成员”:{“id”:464258,“display_name”:“test1”,“mitch_rank_index”:6.64,“距离”:0.56009166639932,“unread_messages_from”:0,“explorer”:假,“ online": true, "favourite": false, "fan": false, "thumbnail_url": "link", "profile_photo_url": "link" } }, { "member": { "id": 1009345, "display_name" :“测试2”,“mitch_rank_index”:6.32,“距离”: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 ,“在线”:真,“最爱”:假,“粉丝”:假,“thumbnail_url”:“链接”,“profile_photo_url”:“链接”}}]

}

4

2 回答 2

2

尝试使用:

json_decode()

您可以在此处阅读有关此功能的更多信息:http: //php.net/manual/en/function.json-decode.php

于 2013-03-17T23:41:06.357 回答
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 />");
}
于 2013-03-17T23:53:54.137 回答