0

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.net.

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"

0

5 Answers 5

4
jsonData.DocumentResponseResults.forEach(function(Result) {
  console.log(Result.Name)
});
Sign up to request clarification or add additional context in comments.

Comments

2
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");
}

Comments

1
$.each( jsonData.DocumentResponseResults , function( k, v )
{
    //here you can access v.Name, v.Id etc.                

});

Comments

1

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.com/jquery.each/

or just a for() loop.

Comments

0

jsonData.DocumentResponseResults is the items property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.