我的json文件有点问题,我要做的是从我的json文件中获取特定对象,所以在上面这个json我添加这个参数 stream_id = 并且例如,我添加此ID stream_id = 200 它应该只显示具有该ID的对象,所以要更清楚它应该显示 id:200,name:Ravi Tamada,email:ravi @ gmail .com 等等,用PHP,谢谢
<head>
<script>window.Polymer = {dom: 'shadow', lazyRegister: 'max', useNativeCSSProperties: true};</script>
<base href="https://polygit.org/polymer+1.7.1/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<style>
p {
--test: brown;
color: var(--test);
}
</style>
<p>Hello world</p>
</template>
</dom-module>
</body>
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:0)
可能你可以使用带有$ assoc = true的json_decode(此选项将json转换为数组,甚至转换为嵌套数组),然后遍历数组以找到所需的元素。
答案 2 :(得分:0)
您应该能够使用如下所示的简单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 "";
});