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

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

本文关键字:JSON 输出 不知道 一个对象      更新时间:2023-09-26
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,但我的问题是价值,如品牌,产品&;其他东西可能不一样。

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

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

我最近不得不做一些类似的事情,最终将键推入一个数组,然后遍历该数组以获得键名。我的解决方案来自这个问题:获取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);
    }
}

可以通过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
    }
}

检查这个提琴

相关文章: