从json(Angular)中的对象数组中检索数据

时间:2020-05-12 11:49:00

标签: arrays json angular object parsing

我有以下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']);

但是不断变得“未定”

有什么办法解决这个问题吗?

1 个答案:

答案 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

相关问题