获得在JSON
问题描述:
嵌套对象的值,我想在下面的JSON的REQ1值,programatically.Here RequestTypeItem也可被改变,所以它是不固定的。我还能使用object.subobject获得在JSON
我能够导航至插槽使用
var b = JSON.parse("{ .... }");
b.request.intent.slots.RequestTypeItem.value
已导航,但我可以进一步编程导航。
{"request": {
"locale": "en-US",
"timestamp": "2016-09-25T00:36:14Z",
"type": {
"name": "request",
"slots": {
"RequestTypeItem": {
"name": "RequestTypeItem",
"value": "req1"
}
}
}
}
}
答
在你的JSON你的要求不具有意图一个属性,它确实有一个属性型,让你,那么你可以用
b.request.type.slots.RequestTypeItem.value
的jsfiddle访问所需的属性:https://jsfiddle.net/9cexbn54/
编辑:再次阅读您的问题后,也许这是你想要的:
// loop through all properties on the slots object
for (var i in b.request.type.slots) {
if (b.request.type.slots.hasOwnProperty(i)) { // make sure it is a property belonging directly to slots, and not "inherited" from the prototype chain
if (b.request.type.slots[i].value) { // make sure that the sub-property of slots has a value property
document.getElementById("output").innerHTML = b.request.type.slots[i].value;
break; // break out of the loop after getting a value
}
}
}
这里我循环遍历插槽上的所有属性,检查属性确实属于插槽,并且它具有值属性。
的jsfiddle:https://jsfiddle.net/9cexbn54/1/
答
我给了其中一些你发现它不是有用的链接。 这里是会得到REQ1:
[email protected]'
[{"request": {
"locale": "en-US",
"timestamp": "2016-09-25T00:36:14Z",
"type": {
"name": "request",
"slots": {
"RequestTypeItem": {
"name": "RequestTypeItem",
"value": "req1"
}
}
}
}
}]
'@
$json=ConvertFrom-Json $data
$json.request.type.slots.RequestTypeItem.value