[英]Fetch data from a complex JSON
I'm new to javascript and JSON and I've been given a task to complete. 我是javascript和JSON的新手,已经完成了一项任务。 Please find the JSON in the following link, 请在以下链接中找到JSON,
http://pastebin.com/0BY3eptF http://pastebin.com/0BY3eptF
According to me the above is a very complex JSON. 根据我的说法,上面是一个非常复杂的JSON。
I'm trying to fetch the out from a WSDL via ajax 我正在尝试通过Ajax从WSDL中获取数据
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. 我想学习如何迭代每个JSON字符串循环。 I also see an array which is WSResult[]. 我还看到了一个数组,它是WSResult []。 A neat javascript with explanation will help me a lot. 带有说明的整洁javascript将对我有很大帮助。 Thank you. 谢谢。
success: function(api) {}
, here, api
is still a string, you have to parse it to JSON first: success: function(api) {}
,这里api
仍然是一个字符串,您必须首先将其解析为JSON:
success: function(api) {
var api = JSON.parse(api);
console.log(api.SearchResult); // trying to fetch information on SearchResult object
}
Some web services return content type as plain text instead of json, you have to manually convert into json. 一些Web服务以纯文本而不是json的形式返回内容类型,您必须手动将其转换为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数组,请找到以下代码
$(api.SearchResult.Result.WSResult).each(function (index, val) { // here val is single object of WSResult array });
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 在此处阅读有关dataType的部分,因为它可能会解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.