使用 Javascript 访问二维 JSON 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13415318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:42:13  来源:igfitidea点击:

Accessing a two dimensional JSON array with Javascript

javascriptarraysjson

提问by Krausers

Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?

可能重复:
我有一个嵌套的数据结构/JSON,我如何访问特定值?

I am using the US Census API and end up with a two dimensional json array from a jQuery.get() request. My result (data) looks like this:

我正在使用美国人口普查 API 并最终从 jQuery.get() 请求得到一个二维 json 数组。我的结果(数据)如下所示:

[["P0010001","NAME","state","county","tract"], ["2703","Census Tract 4001.01","17","119","400101"], ["5603","Census Tract 4001.02","17","119","400102"], ["4327","Census Tract 4002","17","119","400200"]]

[["P0010001","NAME","state","county","tract"], ["2703","Census Tract 4001.01","17","119","400101"], ["5603","Census Tract 4001.02","17","119","400102"], ["4327","Census Tract 4002","17","119","400200"]]

It looks exactly like a two dimensional javascript array, but I cannot access it like one when I try:

它看起来完全像一个二维 javascript 数组,但是当我尝试时,我无法像访问它那样访问它:

var population = data;
alert(population[1][0]);

Is there a way to convert the json array into a javascript array, or to convert it to a string, which could then be put into an array?

有没有办法将json数组转换为javascript数组,或者将其转换为字符串,然后可以将其放入数组中?

回答by Adam

Use JSON.parse:

使用JSON.parse

var population = JSON.parse(data);
alert(population[1][0]);

JsFiddle: http://jsfiddle.net/6CGh8/

JsFiddle:http: //jsfiddle.net/6CGh8/