0

我有这个 json 文件:

{
"categories": [
    {
        "producerName": "Canon",
        "productName": [
            {
                "kamera": "EOS 5D",
                "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
                "kamera": "EOS 5D Mark II",
                "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
                "kamera": "EOS 5D Mark III",
                "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
                "productObj": [
                    {
                        "objektiv": "Canon EF 15mm f/2.8 Fisheye",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    },
                    {
                        "objektiv": "Canon EF 8-15mm f/4L Fisheye USM",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    },
                    {
                        "objektiv": "Canon EF 14mm f/2.8L USM",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    },
                    {
                        "objektiv": "Canon 24mm f/1.4 L USM",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    }
                ]
            },
            {
                "kamera": "EOS 7D",
                "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/",
                "productObj": [
                    {
                        "objektiv": "Canon EF 15mm f/2.8 Fisheye",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    },
                    {
                        "objektiv": "Canon EF 8-15mm f/4L Fisheye USM",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    },
                    {
                        "objektiv": "Canon EF 14mm f/2.8L USM",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    },
                    {
                        "objektiv": "Canon 24mm f/1.4 L USM",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    }
                ]
            }
        ]
    },
    {
        "producerName": "Nikon",
        "productName": [
            {
                "kamera": "D800",
                "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/",
                "productObj": [
                    {
                        "objektiv": "Canon EF 15mm f/2.8 Fisheye",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    }
                ]
            }
        ]
    },
    {
        "producerName": "Olympus",
        "productName": [
            {
                "kamera": "OM-D EM-5",
                "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/",
                "productObj": [
                    {
                        "objektiv": "Canon EF 15mm f/2.8 Fisheye",
                        "link": "/uw-ports-fuer/vollformat/canon-ef-8-15mm-f-4l-fisheye-usm/"
                    }
                ]
            }
        ]
    }
]

}

我通过带有 json 回调的 jQuery 解析这个,效果很好。

唯一的问题是我想将所有带有“link”的“camera”连接到“productObj”,但我总是只得到最后一个字符串。

希望您能够帮助我。

jQuery部分:

$('#model').on("change", function() {
    var indx = $("#cat").find(".selectBox-label").attr("id");
    //console.log(indx);
    var item = '<option id="std">2. Kamera auswählen:</option>';
    for (n=0; n < menu[indx].productName.length; n++){
        item += '<option id="'+[n]+'">' + menu[indx].productName[n].kamera + '</option>';
    }
    $('#camera').html(item);
    $('#camera').selectBox('refresh');
    $('#camera').selectBox('enable');
    $.cookie('m', menu[indx].producerName);
    $.removeCookie('k');
    $.removeCookie('o');


    //second onChange
    $('#camera').on("change", function() {

        var indy = $("#items").find(".selectBox-label").attr("id");
        var goRef = menu[indx].productName[indy].link;

        if(goRef) {

            $.cookie('k', menu[indx].productName[indy].kamera);
            if($(".leftfinder").length > 0) {
                $(".gofind").css({'background': 'url(leftgofind.png)', 'cursor':'pointer'});
            }else {
                $(".gofind").css({'background': 'url(gofindbt.png)', 'cursor':'pointer'});
            }

            $(".gofind").on("click", function() {
                window.location.href = "url"+goRef;
            });
        }
});
4

3 回答 3

0

使用此代码,您的属性将被覆盖:

var obj = { "kamera": "EOS 5D",
    "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
    "kamera": "EOS 5D Mark II",
    "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
    "kamera": "EOS 5D Mark III",
    "link": "/unterwassergehaeuse-fuer/canon-eos-5d/"
}

相当于:

var obj =   {  "kamera": "EOS 5D Mark III",
    "link": "/unterwassergehaeuse-fuer/canon-eos-5d/"}

这就是为什么你只得到最后一个字符串的原因。您不能定义具有多个同名属性的对象

于 2013-06-14T14:47:49.053 回答
0

而不是这种结构:

 { 
   "kamera": "EOS 5D",
   "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
   "kamera": "EOS 5D Mark II",
   "link": "/unterwassergehaeuse-fuer/canon-eos-5d/",
   "kamera": "EOS 5D Mark III",
   "link": "/unterwassergehaeuse-fuer/canon-eos-5d/"
 }

为什么没有:

[
   {
      "kamera": "EOS 5D",
      "link": "/unterwassergehaeuse-fuer/canon-eos-5d/"
   },
   {
      "kamera": "EOS 5D Mark II",
      "link": "/unterwassergehaeuse-fuer/canon-eos-5d/"
   },
   {
      "kamera": "EOS 5D Mark III",
      "link": "/unterwassergehaeuse-fuer/canon-eos-5d/"
   }
]

即对象数组。然后你可以迭代它。

于 2013-06-14T15:05:57.337 回答
0

这不是好的 JSON 结构,你会覆盖你的值,因为你使用相同的键

无论如何,这里是如何解析:

$.each(json.categories, function(key, val) {
    $.each(val.productName, function(key, val) {
        console.log("kamera: " + val.kamera + " | link:" + val.link);
    });
});

jsFiddle 代码

于 2013-06-14T15:06:57.817 回答