我有以下JSON文件
{
"jsonrpc": "2.0",
"id": "1111",
"method": "object-define",
"params": [
{"type": "FEEDBACK","component": "AUDIO", "object": "BUTTON", "id": "1", "state": "ACTIVE"},
{"type": "FEEDBACK","component": "AUDIO", "object": "BUTTON", "id": "2", "state": "ACTIVE"}
]
}
,并试图获取对象的“类型”。 使用以下代码,我可以返回一个对象。 但是,例如,不能选择“类型”或“组件”。
example_protocol = require("../../assets/example_protocol.json");
this.serialSplitted = JSON.stringify(this.example_protocol, ['params']);
this.serialParams = JSON.parse(this.serialSplitted);
this.serialObjects = this.serialParams.params;
console.log(this.serialObjects[0]);
使用
console.log(this.serialObjects[0].type);
或
console.log(this.serialObjects[0]['type']);
但是不断变得“未定”
有什么办法解决这个问题吗?
答案 0 :(得分:0)
请尝试
var example_protocol = {
"jsonrpc": "2.0",
"id": "1111",
"method": "object-define",
"params": [
{"type": "FEEDBACK","component": "AUDIO", "object": "BUTTON", "id": "1", "state": "ACTIVE"},
{"type": "FEEDBACK","component": "AUDIO", "object": "BUTTON", "id": "2", "state": "ACTIVE"}
]
}
var serialSplitted = JSON.stringify(example_protocol);
var serialParams = JSON.parse(serialSplitted);
var serialObjects = serialParams.params;
console.log(serialObjects[0]);
console.log(serialObjects[0].type);
您无需在['params']
期间使用JSON.stringify