0
var myData = {
   "result": "success",
   "theBox": {
      "Brands": [{
         "lastPublishTime": null,
         "id": "e054e3d5-143c-4eab-9fc2-7740edce7d09",
         "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
         "name": "Brand A"
      }, {
         "lastPublishTime": "09:42 Tue Apr 29 2014 BST",
         "id": "402f3c42-3d8d-45d6-8c50-c5d1b5025c23",
         "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
         "name": "Brand B"
      }],
      "Products": [{
         "lastPublishTime": null,
         "id": "db35610c-3148-4b89-856c-66f907206037",
         "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
         "name": "Product 1"
      }],
      "OtherStuff": []
   }
}    

var theTabsNames = (Object.getOwnPropertyNames(data.sandbox));

var arrayLength = theTabsNames.length;
for (var i = 0; i < arrayLength; i++) {
   if (theTabsNames[i] != null) {
      //var tabNumber = [i] + 1;
      //console.log("Number:" +tabNumber);
      var theTabName = theTabsNames[i];
      var voiceSession = data.sandbox.theTabName;

      //console.log("AAA" +voiceSession);

      console.log("Name :" + voiceSession);
      //   var voiceSession = theTabsName[i];
      var arrayLengthL = theTabName.length;
      for (var j = 0; j < arrayLengthL; j++) {

         if (data.sandbox.theTabName[j] != undefined) {
            console.log("Name :" + data.sandbox.Brands[j].name);
            console.log("lastUpdateTime :" + data.sandbox.Brands[j].lastUpdateTime);
            console.log("lastPublishTime :" + data.sandbox.Brands[j].lastPublishTime);
            console.log("Id :" + data.sandbox.Brands[j].id);
         }

      }

      //Do something
   }
}

I have no problem outputting this JSON but my issue is that values such as Brands, Products & OtherStuff might not be the same.

How do I find the names of the Objects then use them here? I can output the actual values but then they don't work when I try to use them to find the nodes.

console.log("Name :" +data.sandbox.Brands[j].name);
Batu.Khan
  • 3,060
  • 2
  • 19
  • 26
  • If you want to get the keys of any object, you can use `Object.keys(anyObject);` like `Object.keys(data.sandbox);` it will return an array like `["Brands","Products","OtherStuff"]`. – Roland Kákonyi May 05 '14 at 09:58

2 Answers2

0

You can get the keys with the help of Object.keys(obj) function

var myData = {
   "result": "success",
   "theBox": {
      "Brands": [{
         "lastPublishTime": null,
         "id": "e054e3d5-143c-4eab-9fc2-7740edce7d09",
         "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
         "name": "Brand A"
      }, {
         "lastPublishTime": "09:42 Tue Apr 29 2014 BST",
         "id": "402f3c42-3d8d-45d6-8c50-c5d1b5025c23",
         "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
         "name": "Brand B"
      }],
      "Products": [{
         "lastPublishTime": null,
         "id": "db35610c-3148-4b89-856c-66f907206037",
         "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
         "name": "Product 1"
      }],
      "OtherStuff": []
   }
}  
var x = Object.keys(myData.theBox)
alert(x.toString())

You can parse through the key names and get the corresponding values with that key

UPDATE

Added the script to get the values

var x = Object.keys(myData.theBox)//Get the Keys
for (var i=0; i < x.length; i++)//Iterate through the keys
{
    var nodesCount = myData.theBox[x[i]].length;//number of such nodes
    for (var j=0; j < nodesCount; j++) {
        alert("Name :" + myData.theBox[x[i]][j].name);//get the values
    }
}

Check this fiddle

A J
  • 2,112
  • 15
  • 24
0

I've recently had to do something similar and ended up pushing the keys into an array then iterating over this array to get the key name. The solution for me came from this question here: Getting JavaScript object key list

I couldn't use Object.keys() because I had to support IE 7 & 8.

Here's an example:

var myData = {
    "result": "success",
    "theBox": {
        "Brands": [{
            "lastPublishTime": null,
            "id": "e054e3d5-143c-4eab-9fc2-7740edce7d09",
            "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
            "name": "Brand A"
        }, {
            "lastPublishTime": "09:42 Tue Apr 29 2014 BST",
            "id": "402f3c42-3d8d-45d6-8c50-c5d1b5025c23",
            "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
            "name": "Brand B"
        }],
        "Products": [{
            "lastPublishTime": null,
            "id": "db35610c-3148-4b89-856c-66f907206037",
            "lastUpdateTime": "09:42 Sun Apr 27 2014 BST",
            "name": "Product 1"
        }],
        "OtherStuff": []
    }
}


var keys = [];
for(var theKey in myData.theBox) {
    keys.push(theKey);
}

for (var i=0; i < keys.length; i++) {
    var arrayLength = myData.theBox[keys[i]].length;

    for (var j=0; j < arrayLength; j++) {
        console.log(keys[i]);
        console.log("Name :" + myData.theBox[keys[i]][j].name);
        console.log("lastUpdateTime :" + myData.theBox[keys[i]][j].lastUpdateTime);
        console.log("lastPublishTime :" + myData.theBox[keys[i]][j].lastPublishTime);
        console.log("Id :" + myData.theBox[keys[i]][j].id);
    }
}
Community
  • 1
  • 1
jwestbrook
  • 71
  • 6