迭代JavaScript结构

时间:2014-04-22 07:31:01

标签: javascript arrays

我正在尝试使用JSON.parse解析数据。解析后我得到的数据低于数据。

如何迭代以下解析数据?解析的数据包含数组的数组。

{
  "AcLedger" : [
    {
      "BillRef" : "B4944",
      "ClientBalance" : 100,
      "ClientCr" : 40,
      "ClientDr" : 140,
      "ContraCode" : "C100",
      "DepositBalance" : 30,
      "DepositCr" : 20,
      "DepositDr" : 50,
      "DisbursementGroup" : "DG",
      "Narrative" : "Test Narrative1",
      "OfficeBalance" : 90,
      "OfficeCr" : 10,
      "OfficeDr" : 100,
      "OutstandingValue" : 200,
      "PostDate" : "/Date(1398149097737)/",
      "PostType" : "MA",
      "Reconciled" : false,
      "Reference" : "Ref001",
      "TransactionNumber" : 100,
      "Undone" : false,
      "VatCode" : "VAT100",
      "VatValue" : 103
    },
    {
      "BillRef" : "B4944",
      "ClientBalance" : 100,
      "ClientCr" : 40,
      "ClientDr" : 140,
      "ContraCode" : "C100",
      "DepositBalance" : 30,
      "DepositCr" : 20,
      "DepositDr" : 50,
      "DisbursementGroup" : "DG",
      "Narrative" : "Test Narrative 2",
      "OfficeBalance" : 90,
      "OfficeCr" : 10,
      "OfficeDr" : 100,
      "OutstandingValue" : 200,
      "PostDate" : "/Date(1398149097737)/",
      "PostType" : "MA",
      "Reconciled" : false,
      "Reference" : "Ref002",
      "TransactionNumber" : 789,
      "Undone" : false,
      "VatCode" : "VAT100",
      "VatValue" : 103
    }
  ],
  "Client" : 100,
  "Deposit" : 0,
  "Office" : 0,
  "Transferable" : 0,
  "UnAllocatedCredit" : 0,
  "UnBilledAntiDisbursement" : 0,
  "UnBilledDisbursement" : 0,
  "UnPaidAntiDisbursement" : 0,
  "UnPaidBills" : 0
}

有人会帮助我实现这个目标吗?

2 个答案:

答案 0 :(得分:0)

对于另一个数组中的数组:

var i, j;
var outerArrayItem, innerArrayItem;
for (i = 0; i < outerArray.length; i++) {
    // do something with the outer array
    outerArrayItem = outerArray[i];
    for (j = 0; j < outerArray[i].length; j++) {
        // do something with the inner array
        innerArrayItem = outerArray[i][j]
    }
}

但我不认为这是另一个数组中的数组。要遍历一个对象(在{}内),你可以使用:

var key, innerKey, item;
for (key in someObject) {
    // do something with that item:
    item = someObject[key];
    for (innerKey in item) {
        // do something with item[innerKey]
    }
}

答案 1 :(得分:0)

数据不是数组数组。它是一个包含许多属性的对象(我称之为“数据”),其中一个属性(“AcLedger”)是一个对象数组。

迭代AcLedger数组的一种方法是:

for (var i = 0; i < data.AcLedger.length; i++) {
    var array_item = data.AcLedger[i];

    // Use array_item
}