JSON中Javascript中的动态关联数组创建

时间:2021-11-25 21:30:14

It sounds a lot more complicated than it really is.

听起来比实际情况复杂得多。

So in Perl, you can do something like this:

所以在Perl中,你可以这样做:

foreach my $var (@vars) {
  $hash_table{$var->{'id'}} = $var->{'data'};
} 

I have a JSON object and I want to do the same thing, but with a javascript associative array in jQuery.

我有一个JSON对象,我想做同样的事情,但在jQuery中使用javascript关联数组。

I've tried the following:

我尝试过以下方法:

hash_table = new Array();

$.each(data.results), function(name, result) {
  hash_table[result.(name).extra_info.a] = result.(name).some_dataset;
});

Where data is a JSON object gotten from a $.getJSON call. It looks more or less like this (my JSON syntax may be a little off, sorry):

其中data是从$ .getJSON调用获得的JSON对象。它看起来或多或少像这样(我的JSON语法可能有点偏,对不起):

{
  results:{
    datasets_a:{
      dataset_one:{
        data:{
          //stuff
        }
        extra_info:{
          //stuff
        }
      }
      dataset_two:{
         ...
      }
      ...
    }
    datasets_b:{
      ...
    }
  }
}

But every time I do this, firebug throws the following error:

但每次我这样做,firebug都会抛出以下错误:

"XML filter is applied to non-xml data"

“XML过滤器应用于非xml数据”

3 个解决方案

#1


3  

I think you can use the JSON response as an associative array. So you should be able to go directly in and use the JSON.

我认为您可以将JSON响应用作关联数组。所以你应该能够直接进入并使用JSON。

Assuming you received the above example:

假设你收到了上面的例子:

$('result').innerHTML = data['results']['dataset_a']['dataset_two']['data'];
// Or the shorter form:
$('result').innerHTML = data.results.dataset_a.dataset_two.data;

Understand that I haven't tested this, but it's safer to use the square brackets with a variable than it is to use parenthesis plus the name with the dot accessor.

理解我没有测试过这个,但是使用带有变量的方括号比使用括号加上带有点访问器的名称更安全。

Your example is failing because of some convoluted logic I just caught.

你的例子是失败的,因为我抓住了一些错综复杂的逻辑。

$.each(data.results), function(name, result) {
     hash_table[result.(name).extra_info.a] = result.(name).some_dataset;
});

Now, the foreach loop goes through the variable data.results to find the internal elements at a depth of 1. The item it finds is given to the lambda with the key of the item. AKA, the first result will be name = "datasets_a" item = object. Following me so far? Now you access the returned hash, the object in item, as though it has the child key in name ... "datasets_a". But wait, this is the object!

现在,foreach循环遍历变量data.results以查找深度为1的内部元素。它找到的项目将使用项目的键赋予lambda。 AKA,第一个结果将是name =“datasets_a”item = object。到目前为止跟着我?现在,您可以访问返回的哈希,即项目中的对象,就好像它具有名称中的子键一样...“datasets_a”。但是等等,这是对象!

If all else fails... write your result JSON into a text field dynamically and ensure it is formatted properly.

如果所有其他方法都失败了......将结果JSON动态写入文本字段并确保其格式正确。

#2


0  

Why would you want to change an array into another array ?-)

为什么要将数组更改为另一个数组? - )

-- why not simply access the data, if you want to simplify or filter, you can traverse the arrays of the object directly !-)

- 为什么不简单地访问数据,如果你想简化或过滤,你可以直接遍历对象的数组! - )

#3


0  

This works. Just dump it into a script block to test.

这很有效。只需将其转储到脚本块中进行测试即可。

    d = {
      'results':{
       'datasets_a':{
          'dataset_one':{
            'data':{
              'sample':'hello'
            },
            'extra_info':{
              //stuff
            }
          },
      'dataset_two':{
            ///
          }
          ///
    },
        'datasets_b':{
         ///
        }
      }
}
alert(d.results.datasets_a.dataset_one.data.sample)

I hope this pasted in correctly. This editor doesn't like my line breaks in code.

我希望这个粘贴正确。这个编辑器不喜欢我在代码中的换行符。

d = {
  'results':{
   'datasets_a':{
      'dataset_one':{
        'data':{
          'sample':'hello'
        },
        'extra_info':{
          //stuff
        }
      },
      'dataset_two':{
        ///
      }
      ///
    },
    'datasets_b':{
     ///
    }
  }
};

alert(d.results.datasets_a.dataset_one.data.sample)

#1


3  

I think you can use the JSON response as an associative array. So you should be able to go directly in and use the JSON.

我认为您可以将JSON响应用作关联数组。所以你应该能够直接进入并使用JSON。

Assuming you received the above example:

假设你收到了上面的例子:

$('result').innerHTML = data['results']['dataset_a']['dataset_two']['data'];
// Or the shorter form:
$('result').innerHTML = data.results.dataset_a.dataset_two.data;

Understand that I haven't tested this, but it's safer to use the square brackets with a variable than it is to use parenthesis plus the name with the dot accessor.

理解我没有测试过这个,但是使用带有变量的方括号比使用括号加上带有点访问器的名称更安全。

Your example is failing because of some convoluted logic I just caught.

你的例子是失败的,因为我抓住了一些错综复杂的逻辑。

$.each(data.results), function(name, result) {
     hash_table[result.(name).extra_info.a] = result.(name).some_dataset;
});

Now, the foreach loop goes through the variable data.results to find the internal elements at a depth of 1. The item it finds is given to the lambda with the key of the item. AKA, the first result will be name = "datasets_a" item = object. Following me so far? Now you access the returned hash, the object in item, as though it has the child key in name ... "datasets_a". But wait, this is the object!

现在,foreach循环遍历变量data.results以查找深度为1的内部元素。它找到的项目将使用项目的键赋予lambda。 AKA,第一个结果将是name =“datasets_a”item = object。到目前为止跟着我?现在,您可以访问返回的哈希,即项目中的对象,就好像它具有名称中的子键一样...“datasets_a”。但是等等,这是对象!

If all else fails... write your result JSON into a text field dynamically and ensure it is formatted properly.

如果所有其他方法都失败了......将结果JSON动态写入文本字段并确保其格式正确。

#2


0  

Why would you want to change an array into another array ?-)

为什么要将数组更改为另一个数组? - )

-- why not simply access the data, if you want to simplify or filter, you can traverse the arrays of the object directly !-)

- 为什么不简单地访问数据,如果你想简化或过滤,你可以直接遍历对象的数组! - )

#3


0  

This works. Just dump it into a script block to test.

这很有效。只需将其转储到脚本块中进行测试即可。

    d = {
      'results':{
       'datasets_a':{
          'dataset_one':{
            'data':{
              'sample':'hello'
            },
            'extra_info':{
              //stuff
            }
          },
      'dataset_two':{
            ///
          }
          ///
    },
        'datasets_b':{
         ///
        }
      }
}
alert(d.results.datasets_a.dataset_one.data.sample)

I hope this pasted in correctly. This editor doesn't like my line breaks in code.

我希望这个粘贴正确。这个编辑器不喜欢我在代码中的换行符。

d = {
  'results':{
   'datasets_a':{
      'dataset_one':{
        'data':{
          'sample':'hello'
        },
        'extra_info':{
          //stuff
        }
      },
      'dataset_two':{
        ///
      }
      ///
    },
    'datasets_b':{
     ///
    }
  }
};

alert(d.results.datasets_a.dataset_one.data.sample)