如何查询具有特定 id 的 json 文件并获取结果数组

How to query a json file with a specific id and get an array of results?

本文关键字:json id 文件 数组 结果 获取 何查询 查询      更新时间:2023-09-26

我正在努力了解如何在循环中根据当前 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 +'&#x2103; - 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 + '
'; } $("#sortable").html(x);});

这样可以更有效地更新每个交付。

然后获取你的临时.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