Reputation: 1601
I'm new to javascript and JSON and I've been given a task to complete. Please find the JSON in the following link,
According to me the above is a very complex JSON.
I'm trying to fetch the out from a WSDL via ajax
success: function(api) {
console.log(api.SearchResult); // trying to fetch information on SearchResult object
}
This doesn't work. I would like to learn how to iterate each JSON string loop. I also see an array which is WSResult[]. A neat javascript with explanation will help me a lot. Thank you.
Upvotes: 0
Views: 135
Reputation: 5093
Not a complete answer, but some useful pointers:
$ajax({
url: 'http://myURL',
// specify the datatype; I think it overrides inferring it from the document MIME type
dataType: 'json',
success: function (api) {
// provided your data does come back as a JSON document
// you should be able to access api.SearchResult
},
error: function( jsXHR, textStatus, errorThrown) {
// always have an error handler, so you can see how it went wrong.
}
);
Read the section on dataType here, as it may solve your problem
Upvotes: 1
Reputation:
Some web services return content type as plain text instead of json, you have to manually convert into json. below code will help you do the same.
success: function(api) { if (api.constructor === String) { api = JSON.parse(api); } console.log(api.SearchResult); }
To loop through api.SearchResult.Result.WSResult array, please find below code
$(api.SearchResult.Result.WSResult).each(function (index, val) { // here val is single object of WSResult array });
Upvotes: 1
Reputation: 35760
success: function(api) {}
, here, api
is still a string, you have to parse it to JSON first:
success: function(api) {
var api = JSON.parse(api);
console.log(api.SearchResult); // trying to fetch information on SearchResult object
}
Upvotes: 1