当您不知道第一个对象的名称时从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,但我的问题是价值,如品牌,产品&;其他东西可能不一样。
我如何找到对象的名称,然后在这里使用它们?我可以输出实际值,但当我试图使用它们来查找节点时,它们不起作用。
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
}
}
检查这个提琴
相关文章:
- 将javascript中的JSON输出为不带jquery的列表
- 如果 CreateJS 库支持 Texture Packer 的 JSON 输出格式
- 通过 PHP 将 JSON 输出转换为 HTML 表
- Javascript/JSON输出不;t显示
- 上传Excel xls,解析到Json,编辑Json输出pdf或Excel xls Nodejs Multer Angu
- HTML页面上的JSON输出
- 使用JSON输出排行榜
- 在表中正确显示json输出
- 简化 JavaScript 对象的 JSON 输出
- 无法让 JSON 输出循环
- 合并位置并从 JSON 输出组合数据
- node-restify:如何缩进 JSON 输出
- JSON 输出的格式不起作用
- Ajax读取PHP脚本而不是JSON输出
- 如何仅使用 JavaScript 的 stringify() 方法在第一级自定义 JSON 输出
- JavaScript JSON 输出到
- 仅绘制 JSON 输出的一部分
- 验证 PHP 中的字符串以进行 JSON 输出
- 由空 json 输出导致的错误“在非对象上调用对象键”
- 如何检查这个由空 json 输出导致的空对象是否真的为空