当您不知道第一个对象的名称时,从JSON输出内容

时间:2014-05-05 09:26:32

标签: javascript json

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

我输出这个JSON没有问题,但我的问题是像Brands,Products&amp; OtherStuff可能不一样。

如何找到对象的名称,然后在这里使用它们?我可以输出实际值,但是当我尝试使用它们来查找节点时它们不起作用。

console.log("Name :" +data.sandbox.Brands[j].name);

2 个答案:

答案 0 :(得分:0)

您可以在Object.keys(obj)函数

的帮助下获取密钥
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())

您可以解析密钥名称并使用该密钥获取相应的值

<强>更新

添加了获取值的脚本

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

选中此fiddle

答案 1 :(得分:0)

我最近不得不做类似的事情,最后将密钥推入数组,然后遍历此数组以获取密钥名称。我的解决方案来自这个问题:Getting JavaScript object key list

我无法使用Object.keys(),因为我必须支持IE 7&amp; 8。

以下是一个例子:

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