如何使用jQuery访问多维JSON数组中的值 [英] How to access values in a multidimensional JSON array with jQuery

查看:71
本文介绍了如何使用jQuery访问多维JSON数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用getJSON提取了一个多维JSON数组,我想访问该数组中的值,但不能超过该数组中的第一个元素.

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.

"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",

...等等.

我的代码:

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

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

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"...}

我在这个论坛上搜索并测试了我所说的不同方法,但是无法解决.

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

推荐答案

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

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

更新

您的json数据似乎无效. (我假设生产者"是数据对象的属性,而该属性的值是所有生产者"对象的数组.)

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.)

我已经创建了这个小提琴( http://jsfiddle.net/kAsPm/)并且可以正常工作如预期的那样.我只需要在生产者"之后将逗号更改为冒号:

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":

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

这篇关于如何使用jQuery访问多维JSON数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆