如何使用 mustache 和 jquery 显示来自大型 json 的单个注册表数据?
How to use mustache and jquery to display a single registry data from a large json?
这是一个 JSON 我从我现在正在处理的系统内部的 "post" 请求中得到的:
{
"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"
}
]
}
我有办法使用 mustache 仅显示来自 id=3 注册表的数据吗?
我用的是小胡子和纯jquery
这是一个小胡子样的模板。您需要找到您的项目,然后解析模板。我没有调用 mustache 库,但应该不会有太大差异。
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>
这是一个 JSON 我从我现在正在处理的系统内部的 "post" 请求中得到的:
{
"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"
}
]
}
我有办法使用 mustache 仅显示来自 id=3 注册表的数据吗?
我用的是小胡子和纯jquery
这是一个小胡子样的模板。您需要找到您的项目,然后解析模板。我没有调用 mustache 库,但应该不会有太大差异。
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>