在javascript中访问数组中的数组
我得到了一个JSON回复,如下所示:在javascript中访问数组中的数组,javascript,jquery,Javascript,Jquery,我得到了一个JSON回复,如下所示: [{ "order_id": "12", "customer": "user user", "status": "Pending", "date_added": "02\/09\/2012", "total": "$500.00", "action": [{ "text": "View", "href": "http:\/\/localhost\/oc\/admin\/index.php?route=sale\/ord
[{
"order_id": "12",
"customer": "user user",
"status": "Pending",
"date_added": "02\/09\/2012",
"total": "$500.00",
"action": [{
"text": "View",
"href": "http:\/\/localhost\/oc\/admin\/index.php?route=sale\/order\/info&token=92a80574e5fcbf3e2d021404cfaae1a4&order_id=12"
}]
}]
看看动作键,它的值又是一个数组。我试图通过以下代码获取操作键值,但它向我显示未定义
function (data) {
if (data) {
for (var i = 0; i < data.length; i++) {
$('div.dashboard-content table.list tbody tr:first').before(
'<tr id="' +
data[i]['order_id'] +
'"><td class="right">' +
data[i]['order_id'] +
'</td><td class="left">' +
data[i]['customer'] +
'</td><td class="left">' +
data[i]['status'] +
'</td><td class="left">' +
data[i]['date_added'] +
'</td><td class="right">' +
data[i]['total'] +
'</td><td class="right"> [<a href="' +
data[i]['action']['href'] + '">' +
data[i]['action']['text'] +
'</a>]</td></tr>'
);
}
}
}
功能(数据){
如果(数据){
对于(变量i=0;i
谁能帮帮我吗。?
提前感谢。正如您所说,
操作
是一个数组。因此,您不能使用数据[i]['action']['href']
访问它。必须使用下标来指示所需数组的位置。例如,要访问第一个位置,可以使用:
var href = data[i].action[0].href;
var text = data[i].action[0].text;
action
是一个数组,其中包含一个对象,该对象具有名为text的属性。更改:
data[i]['action']['text']
致:
你的问题非常不清楚。gawd之母,这些字符串的长度几乎是我在代码中使用的字符串的1/10。我明白为什么有些人对我的代码“不可修改”模糊了。另一方面,您可以在JS中的字符串中换行,在行尾用\转义它们,或者只在
+
符号处换行,这不会影响输出。的可能重复
data[i]['action'][0]['text']