提取 JSON 对象结果的某些部分
我正在尝试解析对 .NET Web 服务的 Ajax 调用的 JSON 结果,如下所示:
function doAjaxCallBack() {
$.ajax({
type: "POST",
url: "AjaxCallBackService.asmx/GetAllTitles",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// show alert book title and tags to test JSON result
},
});
}
这是我从 doAjaxCallBack 返回的 JSON 结果:
{"d":[
{
"__type":"ASP.NET_Training.Book",
"Price":12.3,
"Title":"Javascript Programming",
"Tag":["Ajax","Javascript"]
},
{
"__type":"ASP.NET_Training.Book",
"Price":14.23,
"Title":"Code Complete",
"Tag":["Programming","Concept"]
}
]}
我想要获取书名及其标签。如何循环处理这种 JSON?
谢谢。
I'm trying to parse JSON result from an Ajax call to .NET web service like the following:
function doAjaxCallBack() {
$.ajax({
type: "POST",
url: "AjaxCallBackService.asmx/GetAllTitles",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// show alert book title and tags to test JSON result
},
});
}
Here's the JSON result I got back from doAjaxCallBack:
{"d":[
{
"__type":"ASP.NET_Training.Book",
"Price":12.3,
"Title":"Javascript Programming",
"Tag":["Ajax","Javascript"]
},
{
"__type":"ASP.NET_Training.Book",
"Price":14.23,
"Title":"Code Complete",
"Tag":["Programming","Concept"]
}
]}
I want to get book title and its tags. How do I loop over this kind of JSON?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将返回一个具有一个属性
d
的对象,该属性引用一个对象数组。您可以使用
jQuery.each()
[docs ] 方法迭代该数组,并从数组中的每个对象中选择 Title 和 Tag 属性。实例: http://jsfiddle.net/emSXt/3/ (打开你的控制台)
You're getting back an Object with one property
d
, which references an Array of objects.You can use the
jQuery.each()
[docs] method to iterate over that Array, and select the Title and Tag properties from each Object in the Array.Live Example: http://jsfiddle.net/emSXt/3/ (open your console)
这是一个小提琴http://jsfiddle.net/ATBNx/
Here's a fiddle http://jsfiddle.net/ATBNx/