循环遍历ajax响应中的json对象

时间:2014-09-03 05:28:27

标签: javascript php json

我是json的新手所以我从我的ajax调用中获得了json响应

现在我陷入了循环json对象

这是我的json

 {
    "0": {
        "image": "http://test.com/systems.jpg",
        "anchor_tag_link": "http://test.com/1",
        "title": "Oct-Dec-2013"
    },
    "1": {
        "image": "http://test.com/energy.jpg",
        "anchor_tag_link": "http://test.com/1",
        "title": "July-Sept-2013"
    },
    "pages": 2
}

任何人都可以提供帮助

7 个答案:

答案 0 :(得分:4)

您可以按如下方式使用for-in循环:

var obj = {
  "0": {
    "image": "http://test.com/systems.jpg",
    "anchor_tag_link": "http://test.com/1",
    "title": "Oct-Dec-2013"
},
"1": {
    "image": "http://test.com/energy.jpg",
    "anchor_tag_link": "http://test.com/1",
    "title": "July-Sept-2013"
},
"pages": 2
}

for(var prop in obj) {
    var item = obj[prop];
    console.log(item);
}

请注意,您将获取日志中的项目,因为除数字属性外,您还将获得pages属性。

答案 1 :(得分:2)

将JSON响应保存在变量

var variable = {
    "0" : {
        "image" : "http://test.com/systems.jpg",
        "anchor_tag_link" : "http://test.com/1",
        "title" : "Oct-Dec-2013"
    },
    "1" : {
        "image" : "http://test.com/energy.jpg",
        "anchor_tag_link" : "http://test.com/1",
        "title" : "July-Sept-2013"
    },
    "pages" : 2
};

然后使用jquery

循环它
$.each(variable, function(index, value) {
    alert(value.image);
    alert(value.anchor_tag_link);
});

答案 2 :(得分:1)

你可以这样做。

var json = JSON.parse(data);// here data is your response
    for (var key in json) {

    alert(json[key].image);// other also in the same way.
    }

答案 3 :(得分:0)

示例1:

 success: function(responseData) {
        for (var key in responseData) {
            alert(responseData[key]);
        }
    }

示例2:

<script>
    var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';
    var json = JSON.parse(data);

    alert(json["name"]); //mkyong
    alert(json.name); //mkyong

    alert(json.address.streetAddress); //88 8nd Street
    alert(json["address"].city); //New York

    alert(json.phoneNumber[0].number); //111 111-1111
    alert(json.phoneNumber[1].type); //fax

    alert(json.phoneNumber.number); //undefined
</script>

Sample Code

答案 4 :(得分:0)

请尝试以下代码。您只需要替换&#34; yourJSONObject&#34;通过JSON数组。

    $.each( yourJSONObject, function( keyImg, valImg ) {
            image = valImg[0];
            anchor_tag_link = valImg[1];
            title = valImg[2];
    });

此致

答案 5 :(得分:0)

你可以通过javascript对象循环遍历这个:

   for(var arg in object) {
     object.hasOwnProperty(arg) {
       process(object[arg]); 
     }
   }

答案 6 :(得分:0)

JQuery:
var JSON = {
"0": {
    "image": "http://test.com/systems.jpg",
    "anchor_tag_link": "http://test.com/1",
    "title": "Oct-Dec-2013"
},
"1": {
    "image": "http://test.com/energy.jpg",
    "anchor_tag_link": "http://test.com/1",
    "title": "July-Sept-2013"
},
"pages": 2
};

if(JSON.pages >0)
{
   for(var i=0; i<JSON.pages; i++)
   {
    $('table').append('<tr><td>'+JSON[i].title+'</td><td>'+JSON[i].image+'</td><td>'+JSON[i].anchor_tag_link+'</td></tr>');
   }
}

HTML:
<table border="1"></table>