1

如果我有以下情况:

{"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
}

而且我有一个“hdrs”名称(即“Make”),我如何引用data数组实例?似乎data["Make"][0]应该工作......但无法获得正确的参考

编辑

很抱歉模棱两可.. 我可以循环hdrs获取每个 hdr 名称,但我需要使用每个实例值hdrs来查找其中的所有数据元素data(不确定这是更好的解释)。我会把它放在一个变量中,t因为它是 JSON(感谢重新标记)我希望能够用这样的东西来引用:t.data[hdrs[i]][j]

4

9 回答 9

4

我不得不稍微改变你的代码:

var x = {"hdrs": ["Make","Model","Year"],
         "data" : [ 
           {"Make":"Honda","Model":"Accord","Year":"2008"},
           {"Make":"Toyota","Model":"Corolla","Year":"2008"},
           {"Make":"Honda","Model":"Pilot","Year":"2008"}]
        };

        alert( x.data[0].Make );

编辑:响应您的编辑

var x = {"hdrs": ["Make","Model","Year"],
         "data" : [ 
           {"Make":"Honda","Model":"Accord","Year":"2008"},
           {"Make":"Toyota","Model":"Corolla","Year":"2008"},
           {"Make":"Honda","Model":"Pilot","Year":"2008"}]
        };
var Header = 0; // Make
for( var i = 0; i <= x.data.length - 1; i++ )
{
    alert( x.data[i][x.hdrs[Header]] );
}           
于 2008-09-17T19:49:56.703 回答
2

首先,您忘记了数据数组项中的尾随逗号。

尝试以下操作:

var obj_hash = {
    "hdrs": ["Make", "Model", "Year"],
    "data": [
        {"Make": "Honda", "Model": "Accord", "Year": "2008"},
        {"Make": "Toyota", "Model": "Corolla", "Year": "2008"},
        {"Make": "Honda", "Model": "Pilot", "Year": "2008"},
    ]
};

var ref_data = obj_hash.data;

alert(ref_data[0].Make);

@Kent Fredric:请注意,最后一个逗号不是绝对需要的,但可以让您更轻松地移动行(即,如果您在最后一行之后移动或添加,并且它没有逗号,则必须具体记得加一个)。我认为最好总是有尾随逗号。

于 2008-09-17T19:50:46.717 回答
1

那么,像这样吗?

var theMap = /* the stuff you posted */;
var someHdr = "Make";
var whichIndex = 0;
var correspondingData = theMap["data"][whichIndex][someHdr];

如果我理解正确的话,那应该可以...

于 2008-09-17T19:48:38.050 回答
1
var x = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

x.data[0].Make == "Honda"
x['data'][0]['Make']  == "Honda"

你有你的数组/哈希查找向后:)

于 2008-09-17T19:50:04.350 回答
1

我不确定我是否理解你的问题,但是......

假设上面的 JSON 是 var obj,你想要:

obj.data[0]["Make"] // == "Honda"

如果您只想引用第一个标头引用的字段,则类似于:

obj.data[0][obj.hdrs[0]] // == "Honda"
于 2008-09-17T19:50:55.423 回答
0

也许试试 data[0].Make

于 2008-09-17T19:47:50.360 回答
0

关闭,你会使用

var x = data[0].Make;
var z = data[0].Model;
var y = data[0].Year;
于 2008-09-17T19:48:18.563 回答
0

您显示的代码在语法上不正确;它需要一些逗号。我得到了这个工作:

$foo = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"},
   {"Make":"Toyota","Model":"Corolla","Year":"2008"},
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

然后我可以访问数据:

$foo["data"][0]["make"]
于 2008-09-17T19:50:36.330 回答
0

在答案的帮助下(并且在内部和外部循环正确之后)我得到了这个工作:

var t = eval( "(" + request + ")" ) ;
for (var i = 0; i < t.data.length; i++) {
 myTable +=    "<tr>";
 for (var j = 0; j < t.hdrs.length; j++) {
  myTable += "<td>" ;
   if (t.data[i][t.hdrs[j]] == "") {myTable += "&nbsp;" ; }
    else { myTable += t.data[i][t.hdrs[j]] ; }
  myTable += "</td>";
 }
 myTable +=    "</tr>";
}
于 2008-09-17T20:36:48.713 回答