【问题标题】:Converting JSON to Array Typescript将 JSON 转换为数组 Typescript
【发布时间】:2020-10-29 21:47:15
【问题描述】:

我有以下 JSON

aegisLab: {scan_time: 2, def_time: "2020-07-09T07:57:00Z", scan_result_i: 0, threat_found: ""}
agnitum: null
ahnlab: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
avg: null
avira: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
baidu: null
bitDefender: {scan_time: 8, def_time: "2020-07-09T04:18:00Z", scan_result_i: 0, threat_found: ""}
byteHero: {scan_time: 204, def_time: "2020-07-07T00:00:00Z", scan_result_i: 0, threat_found: ""}
clamAV: {scan_time: 16, def_time: "2020-07-08T13:50:00Z", scan_result_i: 0, threat_found: ""}
cyren: {scan_time: 8, def_time: "2020-07-09T08:43:00Z", scan_result_i: 0, threat_found: ""}
drWebGateway: null
emsisoft: {scan_time: 16, def_time: "2020-07-08T23:38:00Z", scan_result_i: 0, threat_found: ""}
eset: {scan_time: 1, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
filseclab: {scan_time: 173, def_time: "2020-06-22T00:09:00Z", scan_result_i: 0, threat_found: ""}
fortinet: {scan_time: 17, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""}
fprot: null
fsecure: null
hauri: {scan_time: 4, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""}
ikarus: {scan_time: 9, def_time: "2020-07-09T07:45:26Z", scan_result_i: 0, threat_found: ""}
jiangmin: {scan_time: 707, def_time: "2020-07-06T19:06:00Z", scan_result_i: 0, threat_found: ""}
k7: {scan_time: 0, def_time: "2020-07-09T07:29:00Z", scan_result_i: 0, threat_found: ""}
lavasoft: null
mcAfee: {scan_time: 1, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""}
microsoftSecurityEssentials: null
nProtect: null
nanoav: {scan_time: 2, def_time: "2020-07-09T01:49:00Z", scan_result_i: 0, threat_found: ""}
preventon: {scan_time: 47, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""}
quickHeal: null
sophos: {scan_time: 1, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""}
stoPzilla: null
superAntiSpyware: {scan_time: 958, def_time: "2020-07-02T14:29:00Z", scan_result_i: 0, threat_found: ""}
threatTrack: null
totalDefense: null
trendMicro: {scan_time: 486, def_time: "2020-07-07T20:22:00Z", scan_result_i: 0, threat_found: ""}
trendMicroHouseCall: null
virITeXplorer: null
virusBlokAda: {scan_time: 14, def_time: "2020-07-09T08:37:00Z", scan_result_i: 0, threat_found: ""}
xvirusPersonalGuard: null
zillya: null
zoner: null
__proto__: Object

我正在尝试使用以下代码将其转换为数组

this.value.scans = Object.keys(this.value).map(key => {
              this.completed = true;

                    return {
                        ...this.value[key],
                      scannerName: key,

                  };
            });

但是这段代码返回的结果不一致。

【问题讨论】:

  • But this piece of code is returning inconsistent results. 你能详细说明一下吗?究竟是什么不一致?它也不是 JSON,而只是一个常规对象。

标签: arrays json typescript angular8


【解决方案1】:

实际上shared 不是一个有效的对象。每个条目后都缺少逗号。

data ={aegisLab: {scan_time: 2, def_time: "2020-07-09T07:57:00Z", scan_result_i: 0, threat_found: ""},
agnitum: null,
ahnlab: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
avg: null,
avira: {scan_time: 0, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
baidu: null,
bitDefender: {scan_time: 8, def_time: "2020-07-09T04:18:00Z", scan_result_i: 0, threat_found: ""},
byteHero: {scan_time: 204, def_time: "2020-07-07T00:00:00Z", scan_result_i: 0, threat_found: ""},
clamAV: {scan_time: 16, def_time: "2020-07-08T13:50:00Z", scan_result_i: 0, threat_found: ""},
cyren: {scan_time: 8, def_time: "2020-07-09T08:43:00Z", scan_result_i: 0, threat_found: ""},
drWebGateway: null,
emsisoft: {scan_time: 16, def_time: "2020-07-08T23:38:00Z", scan_result_i: 0, threat_found: ""},
eset: {scan_time: 1, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
filseclab: {scan_time: 173, def_time: "2020-06-22T00:09:00Z", scan_result_i: 0, threat_found: ""},
fortinet: {scan_time: 17, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""},
fprot: null,
fsecure: null,
hauri: {scan_time: 4, def_time: "2020-07-09T00:00:00Z", scan_result_i: 0, threat_found: ""},
ikarus: {scan_time: 9, def_time: "2020-07-09T07:45:26Z", scan_result_i: 0, threat_found: ""},
jiangmin: {scan_time: 707, def_time: "2020-07-06T19:06:00Z", scan_result_i: 0, threat_found: ""},
k7: {scan_time: 0, def_time: "2020-07-09T07:29:00Z", scan_result_i: 0, threat_found: ""},
lavasoft: null,
mcAfee: {scan_time: 1, def_time: "2020-07-08T00:00:00Z", scan_result_i: 0, threat_found: ""},
microsoftSecurityEssentials: null,
nProtect: null,
nanoav: {scan_time: 2, def_time: "2020-07-09T01:49:00Z", scan_result_i: 0, threat_found: ""},
preventon: {scan_time: 47, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""},
quickHeal: null,
sophos: {scan_time: 1, def_time: "2020-07-09T03:08:00Z", scan_result_i: 0, threat_found: ""},
stoPzilla: null,
superAntiSpyware: {scan_time: 958, def_time: "2020-07-02T14:29:00Z", scan_result_i: 0, threat_found: ""},
threatTrack: null,
totalDefense: null,
trendMicro: {scan_time: 486, def_time: "2020-07-07T20:22:00Z", scan_result_i: 0, threat_found: ""},
trendMicroHouseCall: null,
virITeXplorer: null,
virusBlokAda: {scan_time: 14, def_time: "2020-07-09T08:37:00Z", scan_result_i: 0, threat_found: ""},
xvirusPersonalGuard: null,
zillya: null,
zoner: null};

public res = Object.keys(this.data).map(key => {
                    return {
                        ...this.data[key],
                      scannerName: key,

                  };
            });

所以我首先更正了您的输入。现在它正在工作

CheckThis

【讨论】:

    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 2018-10-27
    • 2022-01-03
    • 2017-05-30
    • 2022-09-27
    • 2017-02-23
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多