使用jQuery循环JSON结果

时间:2013-09-13 06:58:51

标签: jquery json loops for-loop

我有以下JSON字符串:

{
    "responseHeader":{
        "status":0,
        "QTime":2,
        "params":{
            "facet":"false",
            "fl":"id,title,friendlyurl,avatar,locpath,objectid,objecttype",
            "indent":"off",
            "q":"title_search:*castle*",
            "wt":"json",
            "fq":"userid:\"C325D42C-A777-4275-BDD2-D7810A8AB9AB\"",
            "defType":"lucene"
        }
    },
    "response":{
        "numFound":2,
        "start":0,
        "docs":[
            {
                "title":"castle a",
                "objecttype":1,
                "friendlyurl":"castle-a",
                "avatar":"6_887_castle-a.JPG",
                "objectid":6
            },
            {
                "title":"castle b",
                "objecttype":1,
                "friendlyurl":"castle-b",
                "avatar":"794_360_13j-castle-by-night.jpg",
                "objectid":794
            }
        ]
    }

}

这里有2个搜索结果: 城堡A和城堡B

我想循环遍历所有结果并获取属性title,objectype和friendlyurl

的值

出于测试目的,我只是将JSON字符串分配给变量'data' 然后我尝试了:

    for (var i = 0, l = data.items.length; i < l; i++) {
        console.log(data.items[i].title);
        console.log(data.items[i].objecttype);
        console.log(data.items[i].friendlyurl);
    }

但后来我得到:Uncaught TypeError:无法读取未定义的属性'length'

如果我这样做:

    data = $.parseJSON(data);
    for (var i = 0, l = data.items.length; i < l; i++) {
        console.log(data.items[i].title);
        console.log(data.items[i].objecttype);
        console.log(data.items[i].friendlyurl);
    }

我得到:未捕获的SyntaxError:意外的令牌C jquery-1.8.3.min.js:2

js文件中的行:e.JSON.parse(t);

我也尝试过:

    var data = '{"responseHeader":{"status":0,"QTime":1,"params":{"facet":"false","fl":"id,title,friendlyurl,avatar,locpath,objectid,objecttype","indent":"off","start":"0","q":"title_search:*castle*","wt":"json","fq":"userid:\"C325D42C-A777-4275-BDD2-D7810A8AB9AB\"","rows":"10","defType":"lucene"}},"response":{"numFound":2,"start":0,"docs":[{"title":"castle a","objecttype":1,"friendlyurl":"castle-a","avatar":"6_887_castle-a.JPG","objectid":6},{"title":"castle b","objecttype":1,"friendlyurl":"castle-b","avatar":"794_360_13j-castle-by-night.jpg","objectid":794}]}}'

    var result,
          size = data.result.docs.length,
          index;
    for (index = 0; index < size; index++) {
        result = data.result.docs[index];
        console.log(result.title);
    }

但是会导致错误:未捕获TypeError:无法读取未定义的属性'docs'。

3 个答案:

答案 0 :(得分:3)

这是非常基本的JSON处理。这样的事情应该给你一个开始:

data = $.parseJSON(json);
$.each(data.response.docs, function(i, item) {
    console.log(item.title);
    console.log(item.objecttype);
    console.log(item.friendlyurl);
});

答案 1 :(得分:1)

你不需要jQuery,你可以像这样使用vanilla JavaScript(假设这个json被分配给一个名为json的var)

var result,
      size = json.result.docs.length,
      index;
for(index=0; index<size;index++) {
  result = json.result.docs[index];
  console.log(result.title);
}

答案 2 :(得分:0)

您必须循环遍历所有json对象属性并查找所需的键值。基本操作将包含一个ajax调用,该调用具有调用返回的对象作为参数,然后对于每个循环,您可以检查所需的对象属性。

这是一个应该如何看待基本操作:

$.ajax({ 
    url: 'url_to_json.json',    
    dataType: 'json', // Choosing a JSON datatype
    success: function(data) // Variable data contains the data we get from JSON
    {           
        $.each(data, function(filterGroupKey, filterGroupValue){    
                if (filterGroupValue && filterGroupValue.hasOwnProperty('title')){
                    var filterTagModel = filterGroupValue;                  
                    filterTagModel.title =  filterGroupValue['title'];
                    filterTagModel.objectType =  filterGroupValue['objecttype'] ;
                    filterTagModel.friendlyUrl = filterGroupValue['friendlyurl'];
                }
            });        
    }
});

然后你可以使用filterTagModel及其所有属性来操纵所获得的数据。