unable to get values from JSON converted from XML
Posted
by Dkong
on Stack Overflow
See other posts from Stack Overflow
or by Dkong
Published on 2010-04-21T13:49:44Z
Indexed on
2010/04/21
13:53 UTC
Read the original article
Hit count: 287
I have passed some JSON to my page via a webservice. I used JSON.NET to convert XML to JSON. The JSON output looks ok to me, but I am unable to access some of the items in the response. I am not sure why it is not working. I am using jQuery to read the response and make the webservice call. Even when I try to read the length of the array it says 'undefined'
function GetFeed(){
document.getElementById("marq").innerHTML = '';
$.ajax({
type: "POST",
url: "ticker.asmx/GetStockTicker",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var obj = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
for (var i = 0; i < obj.length; i++) {
$('#marq').html(obj[i].person);
}
}
});
}
This is a copy and paste of my response as it appeared in firebug:
{"d":"{\"?xml\":{\"@version\":\"1.0\",\"@standalone\":\"no\"},\"root\":{\"person\":[{\"@id\":\"1\",\"name\":\"Alan\",\"url\":\"http://www.google.com\"},{\"@id\":\"2\",\"name\":\"Louis\",\"url\":\"http://www.yahoo.com\"}]}}"}
© Stack Overflow or respective owner