我如何访问这些属性?

时间:2011-11-18 19:58:26

标签: jquery ruby-on-rails json

我正在努力解析从这个json中获取每个“division”的“name”和“id”属性到select:

[{"division":{"name":"Solo Male","coed":false,"size":1,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":1}},{"division":{"name":"Solo Female","coed":false,"size":1,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":2}},{"division":{"name":"4 Person Male","coed":false,"size":4,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":9}},{"division":{"name":"4 Person Female","coed":false,"size":4,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":10}},{"division":{"name":"4 Person Coed","coed":true,"size":4,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-10-29T10:46:28-04:00","id":11}},{"division":{"name":"3 Person Male","coed":false,"size":3,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":6}},{"division":{"name":"3 Person Female","coed":false,"size":3,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":7}},{"division":{"name":"3 Person Coed","coed":true,"size":3,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-10-29T10:46:22-04:00","id":8}},{"division":{"name":"2 Person Male","coed":false,"size":2,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":3}},{"division":{"name":"2 Person Female","coed":false,"size":2,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-08-25T16:42:07-04:00","id":4}},{"division":{"name":"2 Person Coed","coed":true,"size":2,"created_at":"2009-08-25T16:42:07-04:00","updated_at":"2009-10-29T10:46:16-04:00","id":5}}]

这是我无法上班的代码:

jQuery.each(data, function(i,division) {
$('#squad_division').append( $('<option value="'+ division.id+'">'+ division.name +'</option>'));

我知道我只是错过了一些简单的地方,我试图访问“division.id”和“division.name”,但它让我望而却步。

如果您能提供帮助,请提前致谢。

干杯

3 个答案:

答案 0 :(得分:1)

啊哈...你的师不是师数据但他们仍然是一个阵列(包含一个师)。

你需要:

jQuery.each(data, function(i,data_item) {
    $('#squad_division').append( $('<option value="'+ data_item.division.id+'">'+ data_item.division.name +'</option>'));

答案 1 :(得分:1)

由于JSON的结构,您需要将division.id更改为division.division.id。以下是几乎完全相同的代码正常运行的jsfiddle:http://jsfiddle.net/RqwBT/1/

基本上你的division变量是数组中的单个键(data变量),它是一个对象,该对象有一个属性:division(具有子属性)你想要访问的。)

jQuery.each(data, function(i,division) {
    $('#squad_division').append( '<option value="'+ division.division.id+'">'+ division.division.name +'</option>');
});

注意我在append语句中删除了HTML周围的$(),因为它会产生不必要的开销。

我通过在每个函数中调用console.log(division);来解决这个问题,这允许我一次检查一个数组键,看看我如何访问对象的信息。当我从新来源加载JSON时,这是我的标准做法。

答案 2 :(得分:1)

代码中的

division(第一个,data[0])是以下对象:

{
  "division": {
     "name":"Solo Male",
     "coed":false,
     "size":1,
     "created_at":"2009-08-25T16:42:07-04:00",
     "updated_at":"2009-08-25T16:42:07-04:00",
     "id":1
  }
}

因此,您需要使用division.division.iddivision.division.name