【问题标题】:Create php script to parse this json [closed]创建 php 脚本来解析这个 json [关闭]
【发布时间】:2013-03-17 23:39:15
【问题描述】:

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

代码如下

{ 成员": [ { “成员”: { “身份证”:464258, "display_name": "test1", “mitch_rank_index”:6.64, “距离”:0.56009166639932, “未读消息来自”:0, “探索者”:错误, “在线”:是的, “最喜欢的”:假的, “粉丝”:假的, "thumbnail_url": "链接", "profile_photo_url": "链接" } }, { “成员”: { “身份证”:1009345, "display_name": "Test2", “mitch_rank_index”:6.32, “距离”:0.583112841628013, “未读消息来自”:0, “探索者”:错误, “在线”:错误, “最喜欢的”:假的, “粉丝”:假的, "thumbnail_url": "链接", "profile_photo_url": "链接" } }, { “成员”: { “身份证”:1052568, “显示名称”:空, “mitch_rank_index”:5.699999999999999, “距离”:0.597684462292014, “未读消息来自”:0, “探索者”:错误, “在线”:是的, “最喜欢的”:假的, “粉丝”:假的, "thumbnail_url": "链接", "profile_photo_url": "链接" } } ]

}

【问题讨论】:

  • StackOverflow 不适合回答这个问题。我们不会为您编写代码。您需要自己编写代码,如果您不确定为什么某些事情没有按预期工作,请发布代码并说明您期望它做什么,以及它实际上在做什么,包括所有错误消息。见about StackOverflow。

标签: php json parsing


【解决方案1】:

尝试使用:

json_decode()

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

【讨论】:

    【解决方案2】:

    这是一个有效的 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 />");
    }
    

    【讨论】:

    • 谢谢我应该怎么做才能只显示来自这个 json 的 ID。
    • 要仅显示此 JSON 中的 ID,请查看 更新的答案
    猜你喜欢
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2012-08-12
    • 2013-08-15
    • 2021-06-13
    相关资源
    最近更新 更多