我有这个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/"
}
]
}
]
}
]
}
我通过jQuery回调解析这个json回调,效果很好。
唯一的问题是我希望所有“kamera”的“link”连接到“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;
});
}
});
答案 0 :(得分: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/"}
这就是为什么你只得到最后一个字符串的原因。您无法使用具有相同名称的多个属性定义对象
答案 1 :(得分: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/"
}
]
即一组对象。然后你可以迭代它。
答案 2 :(得分:0)
这不是一个好的JSON结构,你覆盖你的值,因为你使用相同的键
无论如何这里是如何解析:
$.each(json.categories, function(key, val) {
$.each(val.productName, function(key, val) {
console.log("kamera: " + val.kamera + " | link:" + val.link);
});
});