2017-02-14 66 views
1

我的json文件有点问题,我想要做的是从我的json文件中获取特定对象,因此在此json上面添加此参数时stream_id =例如,我添加了这个ID stream_id = 200它应该只显示该对象的ID,所以要更清楚它应该显示ID:200,名称:Ravi Tamada,电子邮件:[email protected]等等,用PHP,谢谢从特定Id获取特定对象JSON

{ 
     "contacts": [ 
      { 
        "id": "200", 
        "name": "Ravi Tamada", 
        "email": "[email protected]", 
        "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": "[email protected]", 
        "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": "[email protected]", 
        "address": "xx-xx-xxxx,x - street, x - country", 
        "gender" : "male", 


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

@yBrodsky你能解释一下我的代码,请,这是我的JSON所以在PHP中如何做到这一点 –

+0

检查[json_decode()](http://php.net/manual/en/function.json -decode.php) –

回答

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; 
     } 
    } 
} 
+0

是的,但这只是为值为200的id,所以如果我改变了url中的id号码比它应该返回该id的结果,那么如果我愿意channel_id = 201 –

+0

那么这就要达到你改变这个,你可以将200改成'$ _GET ['channel_id']' – Nicolas

+0

我把200改成$ _GET ['channel_id'],这个错误出现了解析错误:语法错误,意外的'AS'(T_AS) ,期待';'在/srv/disk14/2280797/www/rido.sportsontheweb.net/webi/api.php在线61 –

0

可能可以使用json_decode与$ ASSOC =真(此选项的JSON转换成一个阵列,甚至到嵌套数组),然后循环通过阵列查找所需的元件。

+0

它应该取决于在url中写入的值,例如www.nice.com/api?channel_id=,它应该显示结果取决于我给出的值= –

0

您应该可以通过JSON使用下面这样简单的js/jquery函数进行循环,只需传递指定的id即可。

function getJsonById(id){ 
var json = { 
     "contacts": [ 
      { 
        "id": "200", 
        "name": "Ravi Tamada", 
        "email": "[email protected]", 
        "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": "[email protected]", 
        "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": "[email protected]", 
        "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 ""; 
}); 
+0

以及在php中,我该怎么做那 –

+0

我很抱歉,我没有阅读这篇文章的PHP部分。不知道我的头顶PHP语法,但你应该能够使用相同的逻辑。您大概可以从PHP文档网站复制并粘贴。 – Mark