0

How do I pull out ColumnValues[i] using javascript from this JSON sample using two instances with keys = "1" and "2" as shown below. This is just a sample my original JSON object has 500+ entries like this with key = "1", "2", "3", "4", etc. When I retrieve the length of the object the value that is returned equals 1 so using for loop does not work with the length property. Appreciate any help.

JSON object below

[
 {
    "1": {
        "ChildCount": 0,
        "ColumnIndentLevel": 0,
        "DescendantCount": 0,
        "IndentLevel": 0,
        "IsCategory": false,
        "IsConflict": false,
        "IsDocument": true,
        "IsTotal": false,
        "IsValid": true,
        "NoteID": "3962",
        "SiblingCount": 2,
        "UniversalID": "E8D5D7E88B08CBD686257CD6007470E5",
        "ColumnValues": {
            "0": "03297",
            "1": "Amelia Tang",
            "2": "NBK3456",
            "3": "FHA",
            "4": "2008/10/03 00:00:00 UTC-0000",
            "5": "Withdrawn by SASE Administration",
            "6": "Approver Level 2",
            "7": "Reinstatement"
        }
    },
    "2": {
        "ChildCount": 0,
        "ColumnIndentLevel": 0,
        "DescendantCount": 0,
        "IndentLevel": 0,
        "IsCategory": false,
        "IsConflict": false,
        "IsDocument": true,
        "IsTotal": false,
        "IsValid": true,
        "NoteID": "7972",
        "SiblingCount": 2,
        "UniversalID": "4CEE012B8D60A86A86257CD6007657B0",
        "ColumnValues": {
            "0": "20004484",
            "1": "Anthony Susino",
            "2": "",
            "3": "Conventional",
            "4": "2009/05/11 00:00:00 UTC-0000",
            "5": "Withdrawn by SASE Administration",
            "6": "",
            "7": ""
        }
    }
}]
  • Where is the JSON generated from? If it is intended to be an array then it's wrong and should be corrected. Do you control the generation? – Dean Ward Jul 11 '14 at 03:06
  • It's of the length `0` because the outer array has only a single item. – zerkms Jul 11 '14 at 03:07
  • you need to learn bit of javascript on to know object vs arrays and iterating over object properties vs arrays. When you do length on a non array object, you have clearly misunderstood the concept. – gp. Jul 11 '14 at 03:07
  • I would suggest you to use this structure instead: [ {"ChildCount": 0,...}, {"ChildCount": 0,...}, {"ChildCount": 0,....},.... ] – JK ABC Jul 11 '14 at 04:09

2 Answers2

1
var ColumnValues = /* that object */,
    i = 0;         //this makes sure the object is iterated in an ascending order
while(i++ in ColumnValuesd){         //and ignores irrelevant properties that are
    var item = ColumnValues[i];      //inherited from Object's prototype.
    //do something
}

Same goes for the most-top level keys.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • `i++ in ColumnValues` might be a bit more semantically expected. Btw, you're incrementing twice – zerkms Jul 11 '14 at 03:07
1

Use this:

for (var key in yourObject) { // key will be "1", "2", ...
    var value = yourObject[key];

    // do something
}
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • Thanks Robby. Got me going in the right direction. What actually worked for me was `var oJson = ; for(var item in oJson[0]) { alert(oJson[0][item]["ColumnValues"]["1"]); }` – user3827741 Jul 11 '14 at 23:27