繁体   English   中英

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

[英]Outputting content from JSON when you don't know the names of the first Objects

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没问题,但是我的问题是品牌,产品和OtherStuff之类的值可能不相同。

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

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

您可以借助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
    }
}

检查这个小提琴

我最近不得不做类似的事情,最终将键推入数组,然后遍历该数组以获取键名。 对我来说,解决方案来自以下问题: 获取JavaScript对象键列表

我无法使用Object.keys(),因为我必须支持IE 7和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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM