我是PDI的新手,我正在从API中恢复信息,因此我需要对特定结构的信息进行转换,并且不清楚如何通过串联或其他转换来实现。< / p>
这是我与之合作的答案:
[
{
"457": {
"1": {
"value": "4.1",
"timestamp": 1534159593
},
"2": {
"value": "52.2",
"timestamp": 1534159593
},
"3": {
"value": "23.0",
"timestamp": 1534159593
},
"4": {
"value": "250.0",
"timestamp": 1534159593
}
}
}
]
并且我需要保留这种类型的东西,以便通过POST将其发送到另一个API
{
"id": "457",
"type": "greenhouse",
"1": {
"value": 4.1,
"type": "Float",
"timestamp": 1534159593
},
"2": {
"value": 52.2,
"type": "Integer",
"timestamp": 1534159593
},
"3": {
"value": 23.0,
"type": "Integer",
"timestamp": 1534159593
},
"4": {
"value": 250.0,
"type": "Integer",
"timestamp": 1534159593
}
}
感谢您的帮助。
Edit01
Holla,再次
我正在按照你告诉我的方式做,我有问题。
这是我的代码:
// Script here
var data = data2;
var tests = data;
var tests3 = {"457": {"2": {"value": "54.0", "timestamp": 1534246741}, "3": {"value": "22.2", "timestamp": 1534246741}, " 4 ": {" value ":" 260.0 "," timestamp ": 1534246741}," 21 ": {" value ":" 890.0 "," timestamp ": 1534246741}," 1 ": {" value ":" 4.13 "," timestamp ": 1534246741}," 17 ": {" value ":" 194.04687499999997 "," timestamp ": 1534246741}," 5 ": {" value ":" 35.417 "," timestamp ": 1534246741}," 6 ": {" value ":" 26.299999999999997 "," timestamp ": 1534246741}," 8 ": {" value ":" 4.7 "," timestamp ": 1534246741}," 15 ": {" value ":" 0.78 "," timestamp ": 1534246741}," 10 ": {" value ":" 24.94 "," timestamp ": 1534246741}," 22 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 23 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 24 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 26 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 653 ": {" value ":" 0.0 "," timestamp ": 1534246741}," 657 ": {" value ":" - 98.0 "," timestamp ": 1518420299}, "43": {"value": "11.892947103200001", "timestamp": 1534246741}, "42": {"value": "403.61749999999995", "timestamp": 1534246741}}};
var key = Object.keys (data) [0];
var finalobj = {};
for (var and in data [key]) {
finalobj [e] = {
type: "float"
, value: parseFloat (data [key] [e] .value)
, metadata: {
timestamp: {
value: parseInt (data [key] [e] .timestamp)
, type: "Integer"
}
}
};
}
var JsonOutput = JSON.stringify (finalobj);
变量data2是具有我的JSON的变量,它与test3的信息完全相同,如果可以,则代码有效,但是如果我一步一步地将数据放入tests3,由于数据具有相同的值并且具有可以正常工作,并且JsonOutput的响应为{},但如果可以正常运行,我可以通过tests3进行操作。
看起来好像是在获取变量时,但是随后我证明它具有数据和数据2,并且它与tests3的信息相同,我不知道会发生什么。
你能帮我吗?