解析的数据无法保存到mysql

时间:2019-08-21 06:43:19

标签: mysql node.js

json数据是

"{\"n925D\":1,\"t925D\":23.5,\"h925D\":3276.8,...

我将符号替换为

var results = post
    .replace(/\{/g, '[[')
    .replace(/\"/g, '\'')
    .replace(/\,/g, '\'],[')
    .replace(/\:/g, ',\'')
    .replace(/\}/g, '\']]')
    .replace(/\\([^u])/g, '$1')
;

console.log(结果);表演

[['n925D','1'],['t925D','23.5'],['h925D','3276.8'],...

console.log(query.sql);表演

INSERT INTO table (did,val) SET '[[\'n925D\',\'1\'],[\'t925D\',\'23.5\'],[\'h925D\',\'3276.8\'],

实际上如何删除\ ...

1 个答案:

答案 0 :(得分:0)

您无法使用正则表达式可靠地解析JSON(或XML或HTML)。而是parse it, change the data, and re-emit it

var jsonData = '{\"n925D\":1,\"t925D\":23.5,\"h925D\":3276.8,...';

// Parse it into a hash
var jsonParsed = JSON.parse(jsonData); 

// Change the hash in jsonParsed to an array of arrays.

// Convert it back into JSON
jsonData = JSON.stringify(jsonObj);