-2

このjsonデータをphpで解析したいのですが、スクリプトで誰か助けてくれませんか。

以下のコード

{ members": [ { "member": { "id": 464258, "display_name": "test1", "mitch_rank_index": 6.64, "distance": 0.56009166639932, "unread_messages_from": 0, "explorer": false," online": true, "favorite": false, "fan": false, "thumbnail_url": "link", "profile_photo_url": "link" } }, { "member": { "id": 1009345, "display_name" : "テスト 2"、"mitch_rank_index": 6.32, "distance": 0.583112841628013, "unread_messages_from": 0, "explorer": false, "online": false, "favorite": false, "fan": false, "thumbnail_url": "link"," profile_photo_url": "リンク" } }, { "member": { "id": 1052568, "display_name": null, "mitch_rank_index": 5.699999999999999, "distance": 0.597684462292014, "unread_messages_from": 0, "explorer": false 、"online": true, "favorite": false, "fan": false, "thumbnail_url": "link", "profile_photo_url": "link" } } ]

}

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 に答える