如何处理jQuery的阿贾克斯成功的回调我JSON数据? [英] How to handle my JSON data in jQuery Ajax success callback?
问题描述
如果我有一个Ajax调用:
If I have a ajax call:
$.ajax({
url: url,
dataType: 'json',
data: data,
success: function(json_data){
//What's the efficient way to extract the JSON data and get the value
}
});
服务器回到了我的js以下的 JSON 数据。
{"contact":[{"address":[{"city":"Shanghai","street":"Long
Hua Street"},{"city":"Shanghai","street":"Dong Quan
Street"}],"id":"huangyim","name":"Huang Yi Ming"}]}
在我的jQuery的AJAX成功的回调函数,如何提取名称,价值的价值地址(这是对象的列表)优雅?
In my jQuery AJAX success callback function, how to extract the value of "name", value of "address" (which is a list of object) elegantly?
我没有经历过与jQuery和JSON数据处理中的JavaScript。所以,我想问一下如何有效地处理这些数据的一些建议。谢谢你。
I am not experienced with jQuery and JSON data handling in javascript. So, I would like to ask some suggestions on how to handle this data efficiently. Thanks.
推荐答案
一个JSON字符串被解析成一个JavaScript对象/数组。所以,像你访问任何对象的属性,您可以访问的值,数组的元素:
A JSON string gets parsed into a JavaScript object/array. So you can access the values like you access any object property, array element:
var name = json_data.contact[0].name;
var addresses = json_data.contact[0].address;
请访问每个地址里面的值,你可以遍历数组:
Do access the values inside each address, you can iterate over the array:
for(var i = addresses.length; i--;) {
var address = addresses[i];
// address.city
// address.street
// etc
}
如果您有没有JavaScript的这么多的经验,我建议阅读本指南。
If you have not so much experience with JavaScript, I suggest to read this guide.
这篇关于如何处理jQuery的阿贾克斯成功的回调我JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!