如何搜索json结构

时间:2013-10-18 16:00:24

标签: javascript jquery json

我正在尝试通过此json搜索特定值。

我想通过State值搜索,例如'NH'并返回MaintenancePercentage和OfficePercentage。

我已经尝试通过payrollDefaults.State进行循环,但我什么都没回来。

var payrollDefaults =
    [
        {
            "State": "NH",
            "MaintenancePercentage": 21.45,
            "OfficePercentage": 22.56
        },
        {
            "State": "NC",
            "MaintenancePercentage": 21.79,
            "OfficePercentage": 22.41
        },
        {
            "State": "OH",
            "MaintenancePercentage": 21.33,
            "OfficePercentage": 22.25
        },
        {
            "State": "RI",
            "MaintenancePercentage": 23.04,
            "OfficePercentage": 23.22
        },
        {
            "State": "SC",
            "MaintenancePercentage": 21.31,
            "OfficePercentage": 31.33
        },
        {
            "State": "TX",
            "MaintenancePercentage": 35.6,
            "OfficePercentage": 24.48
        },
        {
            "State": "VA",
            "MaintenancePercentage": 32.32,
            "OfficePercentage": 30.47
        },
        {
            "State": "AL",
            "MaintenancePercentage": 13.33,
            "OfficePercentage": 12.42
        },
        {
            "State": "CA",
            "MaintenancePercentage": 49.37,
            "OfficePercentage": 59.67
        },
        {
            "State": "FL",
            "MaintenancePercentage": 34.62,
            "OfficePercentage": 31.06
        },
        {
            "State": "GA",
            "MaintenancePercentage": 35.55,
            "OfficePercentage": 30.29
        },
        {
            "State": "IN",
            "MaintenancePercentage": 33.45,
            "OfficePercentage": 30.69
        },
        {
            "State": "MA",
            "MaintenancePercentage": 32.9,
            "OfficePercentage": 29.34
        },
        {
            "State": "MI",
            "MaintenancePercentage": 34.95,
            "OfficePercentage": 29.46
        },
        {
            "State": "MT",
            "MaintenancePercentage": 35.91,
            "OfficePercentage": 30.02
        }
    ]

编辑:

我最终将其从字符串转换为JSON。

var json = $ .parseJSON(payrollDefaults);

8 个答案:

答案 0 :(得分:1)

你可以使用这个JS lib,DefiantJS(http://defiantjs.com) - 它使用“搜索”方法扩展全局对象JSON。使用此方法,您可以使用XPath表达式搜索JSON结构,并返回一个数组(如果未找到匹配项,则为空)。

var payrollDefaults = [
        { "State": "NH", "MaintenancePercentage": 21.45, "OfficePercentage": 22.56 },
        { "State": "NC", "MaintenancePercentage": 21.79, "OfficePercentage": 22.41 },
        { "State": "OH", "MaintenancePercentage": 21.33, "OfficePercentage": 22.25 },
        { "State": "RI", "MaintenancePercentage": 23.04, "OfficePercentage": 23.22 },
        { "State": "SC", "MaintenancePercentage": 21.31, "OfficePercentage": 31.33 },
        { "State": "TX", "MaintenancePercentage": 35.6,  "OfficePercentage": 24.48 },
        { "State": "VA", "MaintenancePercentage": 32.32, "OfficePercentage": 30.47 },
        { "State": "AL", "MaintenancePercentage": 13.33, "OfficePercentage": 12.42 },
        { "State": "CA", "MaintenancePercentage": 49.37, "OfficePercentage": 59.67 },
        { "State": "FL", "MaintenancePercentage": 34.62, "OfficePercentage": 31.06 },
        { "State": "GA", "MaintenancePercentage": 35.55, "OfficePercentage": 30.29 },
        { "State": "IN", "MaintenancePercentage": 33.45, "OfficePercentage": 30.69 },
        { "State": "MA", "MaintenancePercentage": 32.9,  "OfficePercentage": 29.34 },
        { "State": "MI", "MaintenancePercentage": 34.95, "OfficePercentage": 29.46 },
        { "State": "MT", "MaintenancePercentage": 35.91, "OfficePercentage": 30.02 }
    ],
    res = JSON.search( payrollDefaults, '//*[State = "NH"]' );

console.log( res[0].MaintenancePercentage );
console.log( res[0].OfficePercentage );

这是一个工作小提琴:
http://jsfiddle.net/hbi99/dMLdc/

答案 1 :(得分:0)

您需要为payrollDefaults数组提供一个索引,否则您的程序将无法知道您要从哪个特定对象中提取State的值。

举个例子,这里是如何遍历所有State属性并在控制台中打印它们的值。

for (var i = 0, l = payrollDefaults.length; i < l; i += 1) {
    console.log(payrollDefaults[i].State);
}

注意[i]以及它是如何引用数组中的特定对象的?

希望有助于理解你所缺少的东西!

答案 2 :(得分:0)

这应该可以解决问题......

http://jsfiddle.net/6zn7H/

function findState(state) {
    for(var i =0, l = payrollDefaults.length; i < l; i++){
        var value = payrollDefaults[i];
        if(value.State === state){
            return value;
        }
    }
}

另请注意,此循环仅评估一次长度,而不是每次迭代,有助于提高效率。

答案 3 :(得分:0)

$.each(payrollDefaults,function(key,val){
  if(val.state == "NH"){
   console.log(val.MaintenancePercentage);
   console.log(val.OfficePercentage);
  }

});

这应该可以帮到你

答案 4 :(得分:0)

使用lodash.js找到

_.find(payrollDefaults, {State: 'NH'});
>>> {
        "State": "NH",
        "MaintenancePercentage": 21.45,
        "OfficePercentage": 22.56
    }

答案 5 :(得分:0)

尝试以下功能:

function findByState(givenState) {
    var result = [];
    for(p in payrollDefaults) {
        if (p.State === givenState) {
            result.push(p);
        }
    }

    return result;
}

答案 6 :(得分:0)

以下是搜索特定州的方法

function GetMaintainancePercentage(state) {
    for (var i = 0; i < payrollDefaults.length; i++) {
        if (payrollDefaults[i]["State"] == state) {
            alert('Maintainance Percenatge is ' + payrollDefaults[i]["MaintenancePercentage"]);
        }
    }
}

这是demo

答案 7 :(得分:0)

试试这个: - http://jsfiddle.net/ba5ZQ/1/

<强> JS: -

var arr = jQuery.grep(payrollDefaults, function( a ) {
  return a.State =="NH";
});

console.log(arr);