gpt4 book ai didi

javascript - 如何展平 JSON 嵌套对象,同时保留 Nodejs 中的内部 {}?

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:33 24 4
gpt4 key购买 nike

这是 API 的原始响应。

 {
result: [
{
activeFrom: '2018-02-15T16:02:20.003Z',
activeTo: '2018-02-15T16:02:20.003Z',
distance: 0,
duration: '00:00:00',
rule: [Object],
device: [Object],
diagnostic: [Object],
driver: 'UnknownDriverId',
version: '00000000000061b9',
id: 'a30M9pPNZSEq7DgUdYrF8xw'
},
{
activeFrom: '2018-02-15T16:06:03.003Z',
activeTo: '2018-02-15T16:06:03.003Z',
distance: 0,
duration: '00:00:00',
rule: [Object],
device: [Object],
diagnostic: [Object],
driver: 'UnknownDriverId',
version: '00000000000061bb',
id: 'apk5czda9o06cwRzkSl4_dQ'
}
],
jsonrpc: '2.0',
id: 0
}

我正在使用 npm_flat 展平 JSON API 响应。它正在产生此输出。

var json = JSON.parse(request.responseText);
json = json.result
var json2 = flatten([json], { maxDepth: 15 });
console.log("API Response ", json2);

{
'0.0.activeFrom': '2018-02-15T16:02:20.003Z',
'0.0.activeTo': '2018-02-15T16:02:20.003Z',
'0.0.distance': 0,
'0.0.duration': '00:00:00',
'0.0.rule.id': 'RuleUnauthorizedDeviceRemovalId',
'0.0.device.id': 'b1',
'0.0.diagnostic.id': 'DiagnosticDeviceHasBeenUnpluggedId',
'0.0.driver': 'UnknownDriverId',
'0.0.version': '00000000000061b9',
'0.0.id': 'a30M9pPNZSEq7DgUdYrF8xw',
'0.1.activeFrom': '2018-02-15T16:06:03.003Z',
'0.1.activeTo': '2018-02-15T16:06:03.003Z',
'0.1.distance': 0,
'0.1.duration': '00:00:00',
'0.1.rule.id': 'RuleUnauthorizedDeviceRemovalId',
'0.1.device.id': 'b1',
'0.1.diagnostic.id': 'DiagnosticDeviceHasBeenUnpluggedId',
'0.1.driver': 'UnknownDriverId',
'0.1.version': '00000000000061bb',
'0.1.id': 'apk5czda9o06cwRzkSl4_dQ'
}

我希望输出在 {} 括号内被展平,但保持它们完整,以便它们可以作为“行”插入 BigQuery。

我在删除键中的数字(0.0.)时也遇到了很多麻烦。我已经尝试了很多研究代码,但我缺乏 JS 知识来实现​​这一点。非常感谢任何意见或解决方案。

所需输出:

{
'activeFrom': '2018-02-15T16:02:20.003Z',
'activeTo': '2018-02-15T16:02:20.003Z',
'distance': 0,
'duration': '00:00:00',
'rule.id': 'RuleUnauthorizedDeviceRemovalId',
'device.id': 'b1',
'diagnostic.id': 'DiagnosticDeviceHasBeenUnpluggedId',
'driver': 'UnknownDriverId',
'version': '00000000000061b9',
'id': 'a30M9pPNZSEq7DgUdYrF8xw',
},
{
'activeFrom': '2018-02-15T16:06:03.003Z',
'activeTo': '2018-02-15T16:06:03.003Z',
'distance': 0,
'duration': '00:00:00',
'rule.id': 'RuleUnauthorizedDeviceRemovalId',
'device.id': 'b1',
'diagnostic.id': 'DiagnosticDeviceHasBeenUnpluggedId',
'driver': 'UnknownDriverId',
'version': '00000000000061bb',
'id': 'apk5czda9o06cwRzkSl4_dQ'
}

最佳答案

这应该有效:

let totalResult = []

function formatter(obj) {
let tempResult = {};
function format(obj, position) {
for(let key in obj) {
let val = obj[key];
let newKey = (position ? position + "." + key : key);
if(val && typeof val === "object") {
format(val, newKey);
} else {
tempResult[newKey] = val;
}
}
};
format(obj);
return tempResult;
}

// your example data has no name. Let's pretend it's called "testData"
for (let i = 0; i < testData.result.length; i++) {
totalResult.push(formatter(testData.result[i]));
}

// totalResult now contains your formatted data

关于javascript - 如何展平 JSON 嵌套对象,同时保留 Nodejs 中的内部 {}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59666785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com