如何查询具有特定 id 的 json 文件并获取结果数组
How to query a json file with a specific id and get an array of results?
我正在努力了解如何在循环中根据当前 id 获取 temp.json 的值,请参阅下面的代码 我希望能够根据 temp.json 中匹配的交付 ID(交付 id 和 temp_deliveryid)检索数组。 ,然后将它们打印到表中。temp.json 的内容将不断变化。
交付.json
{
"all_info":[
{
"deliveryid":"1",
"deldescription":"Food Delivery3432",
"delivery_create_time":"2013-09-26 01:19:03",
"routeid":"1",
"startloc":"Newcastle",
"endloc":"Paris",
"distance":"500",
"stockid":"2",
"stockname":"Chicken",
"stockshelf":"20",
"stockdesc":"Newcastle Chickens",
"supplierid":"1",
"supname":"Benny's Fresh Food",
"supaddress":"1 Osborne Road",
"supcity":"Manchester",
"suppostcode":"MA11KL7",
"vehicleid":"1",
"vehiclereg":"TRE12XYZ"
},
{
"deliveryid":"2",
"deldescription":"Fresh Beef",
"delivery_create_time":"2013-09-26 01:19:03",
"routeid":"3",
"startloc":"London",
"endloc":"Niece",
"distance":"800",
"stockid":"1",
"stockname":"Beef",
"stockshelf":"20",
"stockdesc":"Beef from Madrid Farm",
"supplierid":"2",
"supname":"Denone Ltd",
"supaddress":"231 Morje Road",
"supcity":"Barcelona",
"suppostcode":"MR3K12",
"vehicleid":"2",
"vehiclereg":"RFGXMN4"
},
{
"deliveryid":"3",
"deldescription":"Meat Delivery",
"delivery_create_time":"2013-10-14 01:48:59",
"routeid":"3",
"startloc":"London",
"endloc":"Niece",
"distance":"800",
"stockid":"4",
"stockname":"Trout and Salmon",
"stockshelf":"11",
"stockdesc":"FIshers Best Catch",
"supplierid":"4",
"supname":"Youngs Ltd",
"supaddress":"4 Tressle Road",
"supcity":"Telford",
"suppostcode":"TE62GF5",
"vehicleid":"1",
"vehiclereg":"TRE12XYZ"
},
{
"deliveryid":"6",
"deldescription":"Travel Chicken",
"delivery_create_time":"2013-10-16 02:27:26",
"routeid":"4",
"startloc":"Edingburgh",
"endloc":"Malaga",
"distance":"2000",
"stockid":"3",
"stockname":"Rare Meats",
"stockshelf":"45",
"stockdesc":"Expensive Food",
"supplierid":"3",
"supname":"Posh Food Ltd",
"supaddress":"3 Fawn Street",
"supcity":"Warick",
"suppostcode":"WR32B54",
"vehicleid":"1",
"vehiclereg":"TRE12XYZ"
}
]
}
临时.json
{
"temp_info":[
{
"temperatureid":"1",
"recorded_temp":"24",
"weather":"Sunny",
"temp_recorded_time":"2013-09-27 20:54:11",
"temp_deliveryid":"1"
},
{
"temperatureid":"2",
"recorded_temp":"14",
"weather":"Cloudy",
"temp_recorded_time":"2013-09-27 20:54:11",
"temp_deliveryid":"3"
},
{
"temperatureid":"3",
"recorded_temp":"17",
"weather":"Rain",
"temp_recorded_time":"2013-09-27 20:54:51",
"temp_deliveryid":"2"
},
{
"temperatureid":"4",
"recorded_temp":"32",
"weather":"Hot",
"temp_recorded_time":"2013-09-27 20:54:51",
"temp_deliveryid":"1"
}
]
}
网站代码示例
$.getJSON("jsonfiles/deliveries.json", function(delivery) {
for(var i = 0; i < delivery.all_info.length; i++) {
var deliv = delivery.all_info[i];
x = '<div id="' + deliv.deliveryid + '">' + deliv.deldescription + '</div>
document.getElementById("sortable1").innerHTML += x;
$.getJSON("jsonfiles/temp.json", function(temp) {
deliv.forEach(function(entry) {
var tempi = tempe.temp_info[i];
if (tempi.temp_deliveryid == entry.deliveryid){
var y = '<tr><td>[' + temp.temp_recorded_time + '] '+ temp.temperature_id + ' - Temperature: ' + temp.recorded_temp +'℃ - Weather (' + temp.weather + ')!</td></tr>';
document.getElementById("temptbody").innerHTML += y;
});
}
});
});
正如我的评论中所述,如果 deliveries.json 不像 temp.json 那样频繁地更改,我会这样做
$.getJSON("jsonfiles/deliveries.json", function(delivery) { 变量 x =''; for(var i = 0; i <delivery.all_info.length;>' + deliv.deldescription + '
这样可以更有效地更新每个交付。
然后获取你的临时.json
$.getJSON("jsonfiles/temp.json", function(temp) { 变量温度 = {}; var y=''; for(var i = 0; i <temp.temp_info.length;>[' + temp.temp_recorded_time + '] '+ temp.temperature_id + ' - 温度: ' + temp.recorded_temp +'°C - 天气 (' + 温度天气 + ')! '; } $("#temptbody").html(y); });
我想你可能没有解释的是#sortable
和#temptbody
之间的关系。如果存在这样的关系,另一种选择可能是将 deliveries.json 另存为变量,然后在检索 temp.json 时,将适当的临时详细信息直接附加到 deliveries.json
相关文章:
- 根据id将json数组组合为一个json数组
- JSON获取用户id's在嵌套数组中
- 尝试JSON.stringify Ember模型时Id丢失
- tu如何将id放在填充了json数据的html表td上
- 如何通过json对象选项卡中的Id来检查对象是否存在
- 通过知道对象的id在JSON中查找对象
- 通过 id json, jquery 选择元素
- 在 JavaScript 中读取/加载 JSON,其中 'id' = 'x'
- 根据父项和项 ID 重建或重新生成现有 JSON
- 基于变量创建json对象id
- Json:如果类别id与父类别编号匹配,则将类别放在父类别下
- 如何使用json对象作为li元素的id属性或使用javascript锚定标记id
- Ionic从JSON数据中获取整数ID
- 正在读取特定ID的JSON数据
- 主干模型通过ID获取JSON元素
- JSON到带有Django的JS:SyntaxError:缺少:在属性id之后
- JS手册:如何传递(Geo-)json ID到点击事件
- ASP.. NET MVC / JqGrid:是处理和可恢复的JSON Id
- 不能在JavaScript中根据php的json id设置变量
- 如何制作以JSON ID为URL的动态网页-谷歌图表、Fusiontables、Jquery