How to handle my JSON data in jQuery Ajax success callback?

后端 未结 1 1499
北海茫月
北海茫月 2021-02-02 01:56

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 JS         


        
相关标签:
1条回答
  • 2021-02-02 02:30

    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 讨论(0)
提交回复
热议问题