jQuery:处理json响应? [英] Jquery: dealing with a json response?

查看:102
本文介绍了jQuery:处理json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解析来自我的应用程序的响应.这是我最大的猜测如何处理返回的json的电话...

I am having trouble parsing the response from my app. Here is the call with my best guess at how to deal with the json coming back...

$.ajax({
  url: 'houses.json',
  method: 'GET',
  datatype: 'json',
  success: function (data) {
    $.each(data, function (h) {
      $(h).each(function () {
        console.log(h.address);
      });
    });
});

这是我从服务器返回的响应:

Here is the response I am getting back from the server:

[{
  "house": {
    "address": "7 view st dunedin nz",
    "lng": 170.500908,
    "id": 3,
    "lat": -45.875059
  }
}, {
  "house": {
    "address": "26 brown st dunedin nz",
    "lng": 170.496236,
    "lat": -45.875834,
  }
}]

我能得到的最好的答案是不确定的.一次.我正在尝试建立一个循环来为Google地图创建标记. 我可以用另一双眼睛. 任何人? 谢谢.

the best I can get is having it say undefined. Once. I am trying to set up a loop for creating markers for a google map. I could use another pair of eyes. Anyone? Thanks.

推荐答案

由于h是数组,因此需要向下访问属性链,h是具有house对象的集合>属性,因此请对其进行一些更改,如下所示:

Since h is an array, you need to go down the property chain, h is a collection of house objects which have an address property, so change it a bit, like this:

   $.each(data, function(i, h){
     console.log(h.house.address);
   });

请确保删除多余的循环,因为只有一个数组,所以不需要.

Make sure to remove that extra loop around it, no need since there's only one array.

这是一种可视化的思考方式:

Here's a visual way to think of it:

   h       h.house           h.house.address
 [ {       "house":{         "address":"7 view st dunedin nz".....

这篇关于jQuery:处理json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆