【问题标题】:Get specific object from specific Id JSON从特定 ID JSON 中获取特定对象
【发布时间】:2017-07-03 03:38:15
【问题描述】:

我的 json 文件有点问题,我想做的是从我的 json 文件中获取特定对象,所以在上面的这个 json 中,当我添加这个参数 stream_id= 和例如我添加了这个 id stream_id=200 它应该只显示具有该 id 的对象,所以更清楚地说它应该显示 id:200, name:Ravi Tamada, email:ravi@gmail .com 等,用 PHP,谢谢

{
        "contacts": [
            {
                    "id": "200",
                    "name": "Ravi Tamada",
                    "email": "ravi@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",
                    "url": "http://149.202.196.143:8000/live/djemal/djemal/592.ts"
            },
            {
                    "id": "201",
                    "name": "Johnny Depp",
                    "email": "johnny_depp@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",
                    "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
            },
            {
                    "id": "202",
                    "name": "Leonardo Dicaprio",
                    "email": "leonardo_dicaprio@gmail.com",
                    "address": "xx-xx-xxxx,x - street, x - country",
                    "gender" : "male",


    "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
            }
        ]
    }

【问题讨论】:

  • @yBrodsky 你能用代码解释一下吗,这是我的 json 所以在 php 中怎么做

标签: php json


【解决方案1】:

您可能需要更频繁地通过 id 获取数据,而不是每次都循环遍历数组。所以你可以

foreach($users_array as $user_object){
    $users[$user_object->id]=$user_object;
}

然后访问:

echo $users[202]->email;

【讨论】:

    【解决方案2】:

    您可以遍历您的 JSON 并查找您需要的 Id。就这么简单。

    <?php
    $json = /* your json */;
    $array = json_decode($json, true);
    
    $result = getInfo(200, $array);
    
    function getInfo($id, $array) {
        foreach($array AS $index=>$json) {
            if($json['id'] == $id) {
                return $json;
            }
        }
    }
    

    【讨论】:

    • 是的,但这仅适用于值为 200 的 id,所以如果我更改 url 中的 id 编号,它应该返回该 id 的结果,如果我 wright channel_id=201
    • 这取决于你来改变它,你可以把 200 改为 $_GET['channel_id']
    • 我将 200 更改为 $_GET[ 'channel_id' ] 并出现此错误 Parse error: syntax error, unexpected 'AS' (T_AS), expecting ';'在 /srv/disk14/2280797/www/rido.sportsontheweb.net/webi/api.php 第 61 行
    • 哦,将for 替换为foreach
    • 我用您需要的代码制作了一个 PHPSandbox。有sandbox.onlinephpfunctions.com/code/…
    【解决方案3】:

    您应该能够使用如下所示的简单 js/jquery 函数循环遍历 JSON,您只需传递指定的 id。

    function getJsonById(id){
    var json = {
            "contacts": [
                {
                        "id": "200",
                        "name": "Ravi Tamada",
                        "email": "ravi@gmail.com",
                        "address": "xx-xx-xxxx,x - street, x - country",
                        "gender" : "male",
                        "url": "http://149.202.196.143:8000/live/djemal/djemal/592.ts"
                },
                {
                        "id": "201",
                        "name": "Johnny Depp",
                        "email": "johnny_depp@gmail.com",
                        "address": "xx-xx-xxxx,x - street, x - country",
                        "gender" : "male",
                        "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
                },
                {
                        "id": "202",
                        "name": "Leonardo Dicaprio",
                        "email": "leonardo_dicaprio@gmail.com",
                        "address": "xx-xx-xxxx,x - street, x - country",
                        "gender" : "male",
    
    
        "url":"http://149.202.196.143:8000/live/djemal/djemal/592.ts" 
                }
            ]
        };
    
    var l= json['contacts'].length;
    var c = 0;
    while(c<l){
      if(json['contacts'][c]['id']==id){
        console.log(json['contacts'][c]);
         return json['contacts'][c];
      } 
      c++;
    }
    }
    return "";
    });
    

    【讨论】:

    • 在 php 中,我该怎么做
    • 抱歉,我没有阅读这篇文章的 PHP 部分。我不知道 PHP 语法,但您应该能够使用相同的逻辑。您可能只是从 PHP 文档网站复制和粘贴。
    【解决方案4】:

    也许您可以将 json_decode 与 $assoc=true 一起使用(此选项将 json 转换为数组,甚至转换为嵌套数组),然后循环遍历数组以找到所需的元素。

    【讨论】:

    • 它应该取决于写在 url 中的值,例如 www.nice.com/api?channel_id=,它应该根据我给出的值显示结果 =跨度>
    猜你喜欢
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多