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 05 Answers
Reset to default 4jsonData.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
评论列表(0条)