最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to handle my JSON data in jQuery Ajax success callback? - Stack Overflow

programmeradmin0浏览0评论

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
  }
});

Server returned to my js the following JSON data

{"contact":[{"address":[{"city":"Shanghai","street":"Long
            Hua Street"},{"city":"Shanghai","street":"Dong Quan
            Street"}],"id":"huangyim","name":"Huang Yi Ming"}]}

In my jQuery AJAX success callback function, how to extract the value of "name", value of "address" (which is a list of object) elegantly?

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.

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
  }
});

Server returned to my js the following JSON data

{"contact":[{"address":[{"city":"Shanghai","street":"Long
            Hua Street"},{"city":"Shanghai","street":"Dong Quan
            Street"}],"id":"huangyim","name":"Huang Yi Ming"}]}

In my jQuery AJAX success callback function, how to extract the value of "name", value of "address" (which is a list of object) elegantly?

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.

Share Improve this question edited Apr 14, 2011 at 10:14 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Apr 14, 2011 at 10:09 MellonMellon 38.9k78 gold badges192 silver badges265 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 12

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
}

If you have not so much experience with JavaScript, I suggest to read this guide.

发布评论

评论列表(0)

  1. 暂无评论