使用Jquery过滤Json对象

时间:2012-12-06 22:49:12

标签: jquery json .net-2.0

我正在研究Asp.NET(2.0)。我有这样的json响应。

{
  "0": {
    "EMPLOYEE_CODE": "049",
    "EMPLOYEE_NAME": "Craig J Miller",
    "EMPLOYEE_CITY": "Tustin",
    "EMPLOYEE_STATE": "CA"
  },
  "1": {
    "EMPLOYEE_CODE": "050",
    "EMPLOYEE_NAME": "Stephen B",
    "EMPLOYEE_CITY": "FOLSOM",
    "EMPLOYEE_STATE": "CA"
  },
  "2": {
    "EMPLOYEE_CODE": "051",
    "EMPLOYEE_NAME": "Mithali Raj",
    "EMPLOYEE_CITY": "Glendale",
    "EMPLOYEE_STATE": "AZ"
  },
  "3": {
    "EMPLOYEE_CODE": "052",
    "EMPLOYEE_NAME": "Gordon Green",
    "EMPLOYEE_CITY": "Pheonix",
    "EMPLOYEE_STATE": "AZ"
  }
}

现在我想通过EMPLOYEE_STATE过滤上面的json响应作为搜索条件。

我是JSON和jQuery的新手。

2 个答案:

答案 0 :(得分:1)

你可以试试这个

function filter_by_employee_state(query, obj)
{
    var new_obj={}, total=0, query=query.toLowerCase();
    for(var i in obj)
    {
        var emp_st=obj[i].EMPLOYEE_STATE.toLowerCase();
        if(emp_st==query) { new_obj[i]=obj[i]; total++; }
    }
    if(total>0) return new_obj;
    return false;
}

// Filter the data object
var filtered_data=filter_by_employee_state('ca', data);

An Example Here

答案 1 :(得分:0)

当涉及到浏览器时,如果你正在使用Jquery的get或相关方法,那么它将成为一个json对象。

如果您已经知道并且只想过滤数组,请看一下:http://linqjs.codeplex.com

虽然根据您的情况在服务器上过滤它可能更好。