admin管理员组

文章数量:1245895

I am getting a JSON reply, like following:

[{
  "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"
  }]
}]

have a look on action key, it's value is again an array. I am trying to get action key values by following code but it is showing undefined to me

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>'
      );
    }
  }
}

Can somebody help me.? Thanks in advance.

I am getting a JSON reply, like following:

[{
  "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&amp;token=92a80574e5fcbf3e2d021404cfaae1a4&amp;order_id=12"
  }]
}]

have a look on action key, it's value is again an array. I am trying to get action key values by following code but it is showing undefined to me

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>'
      );
    }
  }
}

Can somebody help me.? Thanks in advance.

Share Improve this question edited Sep 2, 2012 at 2:18 Pankaj asked Sep 2, 2012 at 2:06 PankajPankaj 5811 gold badge9 silver badges22 bronze badges 3
  • Your question is terribly unclear. – Pointy Commented Sep 2, 2012 at 2:08
  • Mother of gawd, those strings are almost 1/10 as long as the ones I use in my code. I see why some people dim my code "un-reviewable". On a side-note, you can do line breaks in JS inside strings escaping them with \ at the end of the line or simply line break at the + signs which doesn't affect the output. – Fabrício Matté Commented Sep 2, 2012 at 2:12
  • possible duplicate of I have a nested data structure / JSON, how can access a specific value? – Felix Kling Commented Sep 2, 2012 at 2:13
Add a ment  | 

2 Answers 2

Reset to default 6

Like you said, action is an array. Thus, you can't access it using data[i]['action']['href']. You have to use a subscript to indicate the position of the array that you want. For example, to access the first position, you'd use:

var href = data[i].action[0].href;
var text = data[i].action[0].text;

action is an array containing an object with a property called text. Change:

data[i]['action']['text']

to:

data[i]['action'][0]['text']

本文标签: jqueryAccess array in array in javascriptStack Overflow