这是我从我现在正在处理的系统内的"post"请求中获得的JSON:
{
"return": [
{
"id": 1,
"first_name": "Bonnie",
"last_name": "Little",
"email": "blittle0@acquirethisname.com",
"country": "Portugal",
"ip_address": "48.218.146.63"
},
{
"id": 2,
"first_name": "Judith",
"last_name": "Martin",
"email": "jmartin1@bing.com",
"country": "Sierra Leone",
"ip_address": "233.120.55.214"
},
{
"id": 3,
"first_name": "Carl",
"last_name": "Richardson",
"email": "crichardson2@nsw.gov.au",
"country": "Luxembourg",
"ip_address": "26.228.244.43"
},
{
"id": 4,
"first_name": "Carl",
"last_name": "Richardson",
"email": "crichardson2@nsw.gov.au",
"country": "Luxembourg",
"ip_address": "26.228.244.43"
}
]
}
我有办法使用胡子只显示id=3注册表中的数据吗?
我使用的是胡子和纯jquery
这是一个类似胡子的模板。您需要找到您的项目,然后解析模板。我不需要胡子库,但它应该不会有太大的不同。
var obj = {
"return": [{
"id": 1,
"first_name": "Bonnie",
"last_name": "Little",
"email": "blittle0@acquirethisname.com",
"country": "Portugal",
"ip_address": "48.218.146.63"
}, {
"id": 2,
"first_name": "Judith",
"last_name": "Martin",
"email": "jmartin1@bing.com",
"country": "Sierra Leone",
"ip_address": "233.120.55.214"
}, {
"id": 3,
"first_name": "Carl",
"last_name": "Richardson",
"email": "crichardson2@nsw.gov.au",
"country": "Luxembourg",
"ip_address": "26.228.244.43"
}, {
"id": 4,
"first_name": "Carl",
"last_name": "Richardson",
"email": "crichardson2@nsw.gov.au",
"country": "Luxembourg",
"ip_address": "26.228.244.43"
}]
};
var arr = obj.return;
var i = 0;
var len = arr.length;
var found = null;
for (i = 0; i < len; i++) {
if (arr[i].id == 3) {
found = arr[i];
break;
}
}
if (found != null) {
var el = document.getElementById("output");
var template = el.innerHTML;
template = template.replace(/{{(.*?)}}/g, function(match, name) {
return found[name];
});
el.innerHTML = template;
}
<div id="output">
<b>{{first_name}} {{last_name}}</b> is from <b>{{country}}</b>.
</div>