0

I am trying to parse data using JSON.parse. I am getting below data after parsing.

How can I iterate through below parsed data? The parsed data contains array of an array.

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

Would somebody help me to achieve this?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
user3463768
  • 163
  • 1
  • 2
  • 15
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Qantas 94 Heavy Apr 22 '14 at 07:49
  • The parsed data is an object, that has one property that is, in turn, an array of objects. There is no array of arrays here – Elias Van Ootegem Apr 28 '14 at 11:19

2 Answers2

0

For an array inside another array:

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]
    }
}

but I don't think this is an array inside another array. To traverse an object (inside {}), you would use:

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]
    }
}
Hamza Kubba
  • 2,259
  • 14
  • 12
0

The data is not an array of arrays. It is an object (which I will call 'data') that contains many properties, one of which ("AcLedger") is an array of objects.

One way to iterate through the AcLedger array is:

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

    // Use array_item
}
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • The below code returns data that i have copied.In result i have data. var result = JSON.parse(xmlhttp.responseText).d; when i try to acces var ledgers = result.AcLedger. it says undefined. – user3463768 Apr 22 '14 at 09:59
  • See this [JSFiddle](http://jsfiddle.net/u96SJ/). It will pop up 2 alert dialogs displaying the BillRef properties of each array element. It uses the same data as above, with my suggested solution. – cybersam Apr 22 '14 at 10:43
  • I have updated that data in JsFiddle. My json data is in string format. Can you please check once? – user3463768 Apr 22 '14 at 11:15
  • JSON.parse() does not normally return a string. Please provide the original JSON data and your JSON.parse() call. – cybersam Apr 22 '14 at 15:00