简体   繁体   English

jQuery通过“ where子句”遍历复杂的JSON结构

[英]jquery each iterating over complex JSON structure with “where clause”

I'm not 100% clear how to formulate a $.each() function. 我不是100%清楚如何制定$.each()函数。

The following JSON data block contains multiple items in the array ItemValues . 以下JSON数据块在数组ItemValues包含多个项目。 What I'm looking for is a method (because this packet can be arbitrarily large) is somewhat to get each ItemValue array item WHERE Fields.Id is equivalent to some value "x". 我正在寻找的是一种方法(因为此数据包可以任意大)在某种程度上是为了获取每个ItemValue数组项WHERE Fields.Id等于某个值“ x”。

As well, is there some method to retrieve ItemValues[n].Fields.units as an array? 同样,是否有一些方法可以将ItemValues[n].Fields.units检索为数组?

JSON: JSON:

{
   "CacheName":"Default",
       "LastUpdateTime":"\/Date(1396994130568)\/",
       "Type":"Start",
       "TotalItemsCount":3,
       "PassingFilterCount":3,
       "ItemValues":[
      {
         "Fields":{
            "id":"13288263",
            "status":"8",
            "x_cord":"7250600",
            "y_cord":"566620200",
            "code":"1021",
            "sub_code":"1W",
            "priority":"1",
            "units":"1011, 1130, 1201, 1233, 1445, 1501, 1518, 1714, 1726, 1823, 1825, 1832, 3662",
            "ts":"20140402234025UT"
         },
         "Id":"13288263",
         "LastAction":"add"
      },
      {
         "Fields":{
            "id":"13288264",
            "status":"8",
            "x_cord":"",
            "y_cord":"",
            "code":"",
            "sub_code":"",
            "priority":"-1",
            "units":"",
            "ts":""
         },
         "Id":"13288264",
         "LastAction":"add"
      },
      {
         "Fields":{
            "id":"13288265",
            "status":"8",
            "x_cord":"",
            "y_cord":"",
            "code":"",
            "sub_code":"",
            "priority":"-1",
            "units":"",
            "ts":""
         },
         "Id":"13288265",
         "LastAction":"add"
      }
    ]
}

Assuming your json shown is in variable json , then for this exact scenario you could use: 假设您显示的json在变量json ,那么对于这种确切的情况,您可以使用:

function validItems(id,json){
 var validItemValues = [];
 var values = json.ItemValues;
 for(var value in values){
  if( !values.hasOwnProperty(value) )continue;
  var itemValue = values[value];
  if( itemValue.Fields.id == id ){
   validItemValues.push(itemValue);
  }
 }
 return validItemValues;
}

This same method using jQuery's .each : 使用jQuery的.each相同方法:

function validItems(id,json){
 var validItemValues = [];
 $.each(json.ItemValues,function(){
  if(this.Fields.id == id)validItemValues.push(this);
 });
 return validItemValues;
}

For a recursive approach to finding json values, see this answer: https://stackoverflow.com/a/11657379/1026459 有关查找json值的递归方法,请参见以下答案: https : //stackoverflow.com/a/11657379/1026459

Once you have the valid items, you can iterate through them, and then convert the units string to an array using split. 一旦有了有效项,就可以遍历它们,然后使用split将unites字符串转换为数组。

var validItemList = validItems(id,json);
for( var item in validItemList ){
 if( !validItemList .hasOwnProperty(item ) )continue;
 var currentItem = validItemList[item];
 var fieldsUnitArray = currentItem.Fields.units.split(" ");
 //work with array of units
}

Here's a code which shows it all (using $.each() as you wish), using JSON.parse() and String.split() 这是一个显示所有内容的代码(根据需要使用$.each() ), JSON.parse()String.split()

var json_string = "Your JSON here";

//Use JSON.parse to make it an object
var json_object = JSON.parse(json_string.innerHTML);

//Will store the matched identifier
var matched_id;

//Will store the matched array
var matched_array;

//A valid test case, with units not null
var x = 13288263; 
$.each(json_object.ItemValues, function(key, item_value){
    if(item_value.Fields.id == x){
        matched_id = item_value.Fields.id;

        //Second part of the question
        var units = item_value.Fields.units;
        if(units != ""){
            //String.split() returns an array, by splitting the string with 
            //selected string
            matched_array = units.split(', ');
        }
    }
});

Here's a demo , to play with it. 这是一个演示 ,可以使用它。

Note : there surely are some better solutions out there. 注意:肯定有一些更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM