jquery - 尝试使用 jquery 迭代 JSON 结构

标签 jquery json

我是 jquery 和 JSON 的新手。我有以下 JSON 结构。

{
   "address":{
      "suburb":[
         "HACKHAM WEST",
         "HUNTFIELD HEIGHTS",
         "ONKAPARINGA HILLS",
         "m"
      ],
      "state":"SA"
   }
}

所以基本上以上就是对此的回应:

$.ajax({
    type:'POST',
    url: 'getAddress.php',
    data:postCode='+postCode',
    success: function(response) {
        alert(response)
    }
});

所以,我想要获取的是一个包含状态的变量和一个包含郊区的数组。

最佳答案

检查您是否有有效的 Ajax 请求:

$.ajax({
    type: "POST",
    url: "getAddress.php",
    data: {
        postCode : postCode          // data as object is preferrable
    },
    dataType: "json",                // to parse response as JSON automatically
    success: function(response) {
        var state = response.address.state;
        var suburbs = response.address.suburb;
    }
});

关于jquery - 尝试使用 jquery 迭代 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212950/

相关文章:

javascript - 使用 Jquery 上下文菜单

jquery - 使用 jquery .css 均衡两列的高度

iphone - 如何格式化从 Web 服务返回的字符串数组

c# - 从 BsonDocument 反序列化为字符串并序列化回 BsonDocument

Javascript OOP - jQuery 在 ajax 请求中调用 "this"

jQuery on() 不工作

ios - 将 JSON 数据加载到 TableView 不起作用

javascript - Python 中 Google Maps Direction API 的 JSON 解析

javascript - 如何在 JavaScript 中使用 for 循环检索多个 JSON 对象

php - MySQL查询插入成功但返回false