gpt4 book ai didi

javascript - 自动解析json属性为新对象

转载 作者:行者123 更新时间:2023-11-30 20:59:38 25 4
gpt4 key购买 nike

我有 json 格式的传入数据,如下所示:

原始数据:

{
"data": [{
"id": "Device_6",
"type": "SensingDevice",
"Battery": {
"type": "Number",
"value": "4.08",
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2017-11-09T14:49:02.00Z"
},
"unit": {
"type": "Text",
"value": "voltage"
}
}
},
"DO": {
"type": "Number",
"value": "5.71",
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2017-11-09T14:49:02.00Z"
},
"unit": {
"type": "Text",
"value": "mg/l"
}
}
},
"Humidity": {
"type": "Number",
"value": "57.30",
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2017-11-09T14:49:02.00Z"
},
"unit": {
"type": "Text",
"value": "percent"
}
}
},
"PH": {
"type": "Number",
"value": "14.00",
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2017-11-09T14:49:02.00Z"
}
}
},
"temperature": {
"type": "Number",
"value": "41.00",
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2017-11-09T14:49:02.00Z"
},
"unit": {
"type": "Text",
"value": "celsius"
}
}
},
"waterTemperature": {
"type": "Number",
"value": "29.69",
"metadata": {
"timestamp": {
"type": "DateTime",
"value": "2017-11-09T14:49:02.00Z"
},
"unit": {
"type": "Text",
"value": "celsius"
}
}
}
}
],
"subscriptionId": "59d5eeec4f3db340052d618c"
}

为了特定的应用目的,我需要将这个复杂的 JSON 转换为简单的 key:value 格式。

所以我在带有 node-json-transform 包的 Node js 中使用这段代码:

 var map_data = {
list:'data',
item : { Battery: 'Battery.value',
DO: 'DO.value',
Humidity: 'Humidity.value',
PH: 'PH.value',
temperature: 'temperature.value',
waterTemperature: 'waterTemperature.value', },
};
console.log (map_data)

var dataTransform = DataTransform(data, map_data);
var data_result = dataTransform.transform();

输出看起来像我想要的:

[ { Battery: '4.08',
DO: '5.71',
Humidity: '57.30',
PH: '14.00',
temperature: '41.00',
waterTemperature: '29.69' } ]

但是现在,如果我不知道属性的名称怎么办?所以我试图让它自动发现 key :

var keys = Object.keys(data.data[0]); 
var keysvalues = '';
for (i=2; i < keys.length; i++) {
keysvalues += keys[i]+':"'+ keys[i] + '.value",';
}
var item = '{'+keysvalues+'}';
var map_data = {
list:"data",
item,
};
var dataTransform = DataTransform(data, map_data);
var data_result = dataTransform.transform();

这不起作用,因为对象 map_data.item 是一个“假”对象,我在其中手动添加了 {},因此它被识别为字符串:

console.log(map_data)
{ list: 'data',
item: '{Battery:"Battery.value",DO:"DO.value",Humidity:"Humidity.value",PH:"PH.value",temperature:"temperature.value",waterTemperature:"waterTemperature.value",}' }

你能帮我看看如何让 item 对象成为一个真实的对象,这样我就可以按照我想要的格式自动解析 JSON 吗?

最佳答案

所以基本上你想创建一个给定所有嵌套对象的“值”属性的 map 。这意味着您必须过滤掉不是对象或没有 'value' Prop 的原始对象的键。以下是我的做法(我使用 ES6 来避免冗长)。

const isObject = (o) =>
o instanceof Object && o.constructor === Object;

const getSingleMap = (datum, prop) => {
const mapKeys = Object.keys(datum).filter(key => isObject(datum[key]) && datum[key][prop]);
const obj = {};
mapKeys.forEach(key => {
obj[key] = datum[key][prop];
});

return obj;
}

const getMap = (rawData, prop) =>
rawData.data.map(datum => getSingleMap(datum, prop));

要使用它,只需调用 const res = getMap(rawData, 'value');

/*
res == [
{
"Battery": "4.08",
"DO": "5.71",
"Humidity": "57.30",
"PH": "14.00",
"temperature": "41.00",
"waterTemperature": "29.69"
}
];
*/

下面提供了一个更优雅的getSingleMap实现。

getSingleMap = (datum, prop) => {
const mapKeys = Object.keys(datum).filter(key => isObject(datum[key]) && datum[key][prop]);
return mapKeys.reduce((acc, key) => {
acc[key] = datum[key][prop];
return acc;
}, {});
}

更新:我还用它创建了一个 npm 包,它叫做 json2kv,你可以找到它的来源 here

关于javascript - 自动解析json属性为新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47258891/

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