【问题标题】:Json reading the ApiJson 读取 Api
【发布时间】:2013-12-29 02:57:18
【问题描述】:

我有这个用 jbuilder 生成的 json ruby​​ on rails

{
 "code": "success",
 "place": [
   {
     "id": 1,
     "name": "El Guaton",
     "branches": [

    ]
   },
 {
  "id": 2,
  "name": "Pollo Loco",
  "branches": [
    {
      "id": 6,
      "branch_name": "Sucursal arica",
      "schelude": "lunes a domingo 10:00 a 24:00 horas",
      "adress": "angamos 1115",
      "feature": "Local De Gran espacio  con Eventos",
      "city": {
        "city": "Arica",
        "created_at": "2013-12-15T19:12:43Z",
        "id": 3,
        "region_id": 3,
        "updated_at": "2013-12-15T19:12:43Z"
      },
      "latitude": "-18.478042",
      "longitude": "-70.310667",
      "barcode": "mForqSLGdX",
      "type_place": {
        "created_at": "2013-12-15T19:13:03Z",
        "id": 2,
        "place_name": "Temático",
        "updated_at": "2013-12-15T19:13:03Z"
      }
    },
    {
      "id": 7,
      "branch_name": "sucursal cambio",
      "schelude": "lunes a domingo 24 horas",
      "adress": "los pajaritos",
      "feature": "lugar muy amplio",
      "city": {
        "city": "Concepción",
        "created_at": "2013-12-15T19:12:35Z",
        "id": 2,
        "region_id": 2,
        "updated_at": "2013-12-15T19:12:35Z"
      },
      "latitude": "12.77",
      "longitude": "33.666",
      "barcode": "IqyXwFDyAZ",
      "type_place": {
        "created_at": "2013-12-15T19:13:42Z",
        "id": 4,
        "place_name": "Restaurant",
        "updated_at": "2013-12-15T19:13:42Z"
      }
    }
  ]
},
{
  "id": 3,
  "name": "Sibaritico",
  "branches": [
    {
      "id": 2,
      "branch_name": "Sucursal Viña Centro",
      "schelude": "lunes a viernes 11:00 a las 21:00",
      "adress": "4 norte #523",
      "feature": "Local De Gran espacio  con Eventos",
      "city": {
        "city": "Viña del Mar",
        "created_at": "2013-12-15T19:12:19Z",
        "id": 1,
        "region_id": 1,
        "updated_at": "2013-12-15T19:12:19Z"
      },
      "latitude": "-33.019171",
      "longitude": "-71.55284",
      "barcode": "LjsLuFHoFI",
      "type_place": {
        "created_at": "2013-12-15T19:13:42Z",
        "id": 4,
        "place_name": "Restaurant",
        "updated_at": "2013-12-15T19:13:42Z"
      }
    }
  ]
}
]
}

我用下面的代码阅读了它

function pedirDatos(){

    $.ajax({
    url: "http://bajon.herokuapp.com/api/get_places.json",
    type: "GET",
    dataType: "json",
    success: function(place){ 
    for(i in place['place'])
                {alert(place['place'][i].name);
                alert(place['place'][i].branches[i].branch_name);

                    }

                },

            error:function(data){
                 alert('Error');
                }
            });

} 

第一个警报:alert(place['place'][i].name); // 得到“El Guaton”,下一个“Pollo Loco”,最后是“Sibaritico”,一切都很好..但是第二个警报没有得到数据:(。 如何从分支中检索数据? 感谢您的帮助。

【问题讨论】:

    标签: javascript ruby-on-rails json


    【解决方案1】:

    您正在尝试使用places 数组迭代器来迭代branchs 数组。您需要一个单独的分支数组迭代器。

    for (var i = 0, l = place.place.length; i < l; i++) {
                    alert(place.place[i].name);
        for (var k = 0, len = place.place[i].branches.length; k < len; k++) {
                    alert(place.place[i].branches[k].branch_name);
        }
    }
    

    那会产生

    El Guaton 
    Pollo Loco 
    Sucursal arica 
    sucursal cambio 
    Sibaritico 
    Sucursal Viña Centro 
    

    Operational JSFiddle

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2018-04-28
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      相关资源
      最近更新 更多