以下是源文件中的数据:
{
"dubbuseqchapter+block@a7a5931f68d0482eaff2b7c9f9684e47": {
"category": "chapter",
"children": [
"dubbuseqsequential+block@968513c8f0cc4249b7cfc2290ac967dc",
"dubbuseqsequential+block@f7f730a478144a74bd127f996d6dc4f5",
"dubbuseqsequential+block@91a0d5d7cd9649a3bdf057400e0a1c96",
"dubbuseqsequential+block@28b2b171b6734b13af29735796c5ad5a",
"dubbuseqsequential+block@192a150c8aab43b9bd236773ba60b414",
"dubbuseqsequential+block@26b3464dad42460ea66f9afe89770065"
],
"metadata": {
"display_name": "Introduction course orientation"
}
},
"dubbuseqchapter+block@b2451e9195c5466db8b66f53ed06c9fd": {
"category": "chapter",
"children": [
"dubbuseqsequential+block@c95826a16f71405ba58319d23d250fc4",
"dubbuseqsequential+block@fe4e3b8b7cdd4fa0b9fe9090223b7125",
"dubbuseqsequential+block@44bbdee625dc465ebe725d2126ed0662",
"dubbuseqsequential+block@8d4daba07d4443f3b2a0b2506280ee2c",
"dubbuseqsequential+block@c68d9d3ba7de45b1b0770085e4f1f286",
"dubbuseqsequential+block@ccdca5b2aca94dbdabb3a57a75adf3fa"
],
"metadata": {
"display_name": "Module closing section"
}
}
}
以下javascript带来了顶级键值(即dubbuseqchapter + block @ a7a5931f68d0482eaff2b7c9f9684e47,dubbuseqchapter + block @ b2451e9195c5466db8b66f53ed06c9fd)
Javascript代码
var obj = JSON.parse(jContent);
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var row = createRowCopy(getOutputRowMeta().size());
var idx = getInputRowMeta().size();
row[idx++] = keys[i];
// Alert (keys.length);
putRow(row);
}
但是,在这个例子中,我无法获得密钥的值..(即类别,子元素和元数据)。
我尝试过Objects.values()但它在Alert中返回null或object对象。
答案 0 :(得分:5)
keys
是一个字符串数组,每个字符串都是属性名。
您可以通常的方式获取属性名称的值:
object[property_name]
即
var value = obj[keys[i]];
答案 1 :(得分:0)
此代码显示如何导航到已解析的JsonData
var obj = JSON.parse(textJson);
var keys = Object.keys(obj);
console.log(obj[keys[0]].metadata.display_name);
这将打印:简介课程方向
答案 2 :(得分:0)
甚至可以通过这种方式检索您的subProperties
var obj = JSON.parse(textJson);
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++){
console.log(keys[i]);
var subKeys = Object.keys(obj[keys[i]]);
for (var j = 0; j < subKeys.length; j++) console.log(subKeys[j] + " --> " + obj[keys[i]][subKeys[j]]);
}