如何使用 jQuery 访问多维 JSON 数组中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13959943/
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-08-26 13:08:08  来源:igfitidea点击:

How to access values in a multidimensional JSON array with jQuery

jqueryjsonmultidimensional-arraygetjson

提问by oskarno

So I fetch a multidimensional JSON array with getJSON and I want to access values in the array, but can't reach longer than the first element in the array.

因此,我使用 getJSON 获取了一个多维 JSON 数组,并且我想访问数组中的值,但其长度不能超过数组中的第一个元素。

"producers",
[
    {
        "producer":{
            "id":"1",
            "name":"Em\u00e5mejeriet",
            "address":" Grenv\u00e4gen 1-3",
            "zipcode":" 577 39",
            "district":" Hultsfred",
            "webpage":"http:\/\/www.emamejeriet.se",
            "logoURL":"..\/producenter\/images\/ema.png",
            "latitude":"57.4999",
            "longitude":"15.828"
        }
    },
    {
        "producer":{
            "id":"2",

...and so on.

...等等。

My code:

我的代码:

$.getJSON("/url/producers.json", function(data) {
    $.each(data, function() {
        $.each(this, function(key, value) {
            console.log(value.producer);
        });
    });
});

The output is an object, I don't know any better way to paste it, copied from the browsers console: "> Object {id: "1", name: "Em?mejeriet", address: "Grenv?gen 1-3, zipcode: "577 39", disctrict: "Hulsfred"...}

输出是一个对象,我不知道有什么更好的方法来粘贴它,从浏览器控制台复制:”> Object {id: "1", name: "Em?mejeriet", address: "Grenv?gen 1-3, zipcode: "577 39", disctrict: "Hulsfred"...}

I've searched and tested different approaches as I said, from this forum, but couldn't get my head around it.

正如我所说,我已经从这个论坛搜索并测试了不同的方法,但无法理解它。

回答by the_marcelo_r

Open your firebug's console, paste the following and execute:

打开您的萤火虫控制台,粘贴以下内容并执行:

var json = '{' + 
'"peopleList":[' +
'{"id":1,"name":"marcelo","address":"rua meh","phone":"85 99999999"},'+
'{"id":2,"name":"marcelo2","address":"rua meh2","phone":"85 9999.9999"}]}';

var parsed = JSON.parse(json);

Now, as you will have two objects under the second level of your JSON, you need to specify the object and then you will see that it will allow you to select its properties:

现在,由于您将在 JSON 的第二级下有两个对象,您需要指定该对象,然后您将看到它允许您选择其属性:

parsed.peopleList[0].name

Before working with the dynamically generated JSON, get some dummy data for testing purposes (with the same structure, of course) and play with it before implementing the final code.

在使用动态生成的 JSON 之前,获取一些用于测试目的的虚拟数据(当然具有相同的结构)并在实现最终代码之前使用它。

回答by swatkins

If the output of value.produceris an object, then you should be able to access the properties of that object. For example, value.producer.nameshould output 'asdfsadf'.

如果 的输出value.producer是一个对象,那么您应该能够访问该对象的属性。例如,value.producer.name应该输出'asdfsadf'。

UPDATE

更新

It looks as though your json data is not valid. (I'm assuming "producers" is a property of the data object and the value of that property is an array of all of the "producer" objects.)

看起来您的 json 数据无效。(我假设“生产者”是数据对象的一个​​属性,该属性的值是所有“生产者”对象的数组。)

I've created this fiddle ( http://jsfiddle.net/kAsPm/) and it works as expected. I just had to change the comma to a colon after "producers":

我已经创建了这个小提琴(http://jsfiddle.net/kAsPm/)并且它按预期工作。我只需要在“生产者”之后将逗号更改为冒号:

"producers":  // changed from "producers",
[
    {
        "producer":{
            "id":"1",
            ...

回答by user1541503

I used tempo.js rendering engine, it does multidimensional array rendering. here is the link http://tempojs.com/

我使用 tempo.js 渲染引擎,它进行多维数组渲染。这是链接 http://tempojs.com/

download tempo.cs and follow the link .

下载 tempo.cs 并点击链接。