明树Git Lab

Commit 9e19de6e authored by zfp1's avatar zfp1

update

parent d7334222
let a = `0[N82V3VWYU84K36UH{"sensorDatas":[{"flag":"PM2.5","value":10}, {"flag":"PM10","value":12}]}0vN82V3VWYU84K36UH{"sensorDatas":[{"flag":"O3","value":0}, {"flag":"HUM","value":71.2}, {"flag":"TEMP","value":31.6}]}` let a = `{"sensorDatas":[{"flag":"TEMP","value":33.1}, {"flag":"PM2.5","value":8}, {"flag":"PM10","value":10}]}0X`;
console.log(a.slice(0, -2))
\ No newline at end of file
function genInsertObj(gbkString, deviceNumber) {
// 0[N82V3VWYU84K36UH{"sensorDatas":[{"flag":"PM10","value":56}, {"flag":"PM2.5","value":35}]}0yN82V3VWYU84K36UH{"sensorDatas":[{"flag":"TEMP","value":26.5}, {"flag":"O3","value":0.02}, {"flag":"HUM","value":78.5}]}
let splitArr = gbkString.split(deviceNumber);
let arr = [];
for (let index = 0; index < splitArr.length; index++) {
let element = splitArr[index];
if (element.startsWith('{')) {
if (!element.endsWith('}')) {
element = element.slice(0, -2);
}
arr.push(element)
}
}
console.log(arr)
let obj = {
deviceNumber: deviceNumber,
time: new Date(),
del: 0,
createdAt: new Date(),
updatedAt: new Date(),
}
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
try {
console.log(element)
let jsonObj = JSON.parse(element);
console.log(jsonObj, "===")
let sensorDatas = jsonObj.sensorDatas;
for (let i = 0; i < sensorDatas.length; i++) {
const s = sensorDatas[i];
if (s.flag == "PM10") {
obj.pm10 = s.value;
}
if (s.flag == "PM2.5") {
obj.pm25 = s.value;
}
if (s.flag == "TEMP") {
obj.temp = s.value;
}
if (s.flag == "HUM") {
obj.hum = s.value;
}
if (s.flag == "O3") {
obj.O3 = s.value;
}
}
} catch (error) {
console.log("JSON解析出错", error,)
}
}
return obj;
}
console.log(genInsertObj(a, "N82V3VWYU84K36UH"));
\ No newline at end of file
...@@ -91,7 +91,7 @@ function genInsertObj(gbkString, deviceNumber) { ...@@ -91,7 +91,7 @@ function genInsertObj(gbkString, deviceNumber) {
console.log(1, "--") console.log(1, "--")
if (!element.endsWith('}')) { if (!element.endsWith('}')) {
console.log(2, "--", String(element)) console.log(2, "--", String(element))
let nStr = String(element).slice(0, -2); let nStr = `${element}`.slice(0, -2);
console.log(3, "--", nStr, "======") console.log(3, "--", nStr, "======")
arr.push(nStr) arr.push(nStr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment