简体   繁体   English

如何在jQuery Ajax成功回调中处理我的JSON数据?

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

If I have a ajax call: 如果我有一个ajax电话:

$.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 服务器将以下JSON数据返回给我的js

{"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? 在我的jQuery AJAX成功回调函数中,如何提取“ name ”的值,“ address ”的值(这是一个对象列表 )优雅?

I am not experienced with jQuery and JSON data handling in javascript. 我对javascript中的jQuery和JSON数据处理没有经验。 So, I would like to ask some suggestions on how to handle this data efficiently. 所以,我想问一些有关如何有效处理这些数据的建议。 Thanks. 谢谢。

A JSON string gets parsed into a JavaScript object/array. JSON字符串被解析为JavaScript对象/数组。 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 . 如果您对JavaScript没有太多经验,我建议您阅读本指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM