Iterate JSON Response with JavascriptJQuery - Stack Overflow

I have the following JSON response that i am trying to iterate with Javascript :-{"DocumentRespons

I have the following JSON response that i am trying to iterate with Javascript :-

{
    "DocumentResponseResults": [
        {
            "Name": "Example1",
            "Id": "1"
        },
        {
            "Name": "Example2",
            "Id": "2"
        },
        {
            "Name": "Example3",
            "Id": "3"
        }
    ]
}

I've tested to makes sure its valid JSON using an online validator.

I'm receiving this via a response from a WebMethod in asp.

In the response it looks like this:-

d:"{"DocumentResponseResults":[{"Name":"Example1","Id":"1"},{"Name":"Example2","Id":"2"},{"Name":"Example3","Id":"3"}]}"

I'm trying to iterate the items in the JSON string.

I started off by parsing it like this :-

var jsonData = JSON.parse(response.d);

I've tried to iterate the array of objects by accessing the Items collection, but javascript informs me that the items property is undefined.

Can anyone advise how i iterate the collection, and pull out properties such as "Name" & "Id"

I have the following JSON response that i am trying to iterate with Javascript :-

{
    "DocumentResponseResults": [
        {
            "Name": "Example1",
            "Id": "1"
        },
        {
            "Name": "Example2",
            "Id": "2"
        },
        {
            "Name": "Example3",
            "Id": "3"
        }
    ]
}

I've tested to makes sure its valid JSON using an online validator.

I'm receiving this via a response from a WebMethod in asp.

In the response it looks like this:-

d:"{"DocumentResponseResults":[{"Name":"Example1","Id":"1"},{"Name":"Example2","Id":"2"},{"Name":"Example3","Id":"3"}]}"

I'm trying to iterate the items in the JSON string.

I started off by parsing it like this :-

var jsonData = JSON.parse(response.d);

I've tried to iterate the array of objects by accessing the Items collection, but javascript informs me that the items property is undefined.

Can anyone advise how i iterate the collection, and pull out properties such as "Name" & "Id"

Share Improve this question asked Jun 1, 2015 at 8:26 DerekDerek 8,64013 gold badges61 silver badges93 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 4
jsonData.DocumentResponseResults.forEach(function(Result) {
  console.log(Result.Name)
});
var items = response.DocumentResponseResults //gets you array
for (var i = 0; i < items .length; i++) { 
    var name = items[i].Name;
    var id = items[i].Id;
    console.log("Name:"+name + " Id:"+id+"\n");
}
$.each( jsonData.DocumentResponseResults , function( k, v )
{
    //here you can access v.Name, v.Id etc.                

});

you can access the array this way:

DocumentResponseResults[i].Name

DocumentResponseResults[i].Id

where 'i' is a number.

if you want to go through the array, you can use jQuery.each()

http://api.jquery./jquery.each/

or just a for() loop.

jsonData.DocumentResponseResults is the items property.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330147a4622839.html

相关推荐

  • Iterate JSON Response with JavascriptJQuery - Stack Overflow

    I have the following JSON response that i am trying to iterate with Javascript :-{"DocumentRespons

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信