处理JSON对象异常

This commit is contained in:
aixianling
2023-03-01 18:10:08 +08:00
parent ebcfff03cc
commit c221ce8965
2 changed files with 12 additions and 3 deletions

View File

@@ -1,4 +1,11 @@
const checkJson = str => { const checkJson = str => {
if (typeof str == 'object') {
try {
str = JSON.stringify(str)
} catch (e) {
return false
}
}
if (typeof str == 'string') { if (typeof str == 'string') {
try { try {
let obj = JSON.parse(str); let obj = JSON.parse(str);

View File

@@ -25,8 +25,10 @@ const insert = ({table, form}) => {
if (form.id) {//编辑 if (form.id) {//编辑
let arr = Object.keys(form).filter(e => form[e]).map(e => { let arr = Object.keys(form).filter(e => form[e]).map(e => {
if (typeof form[e] == "object") { if (typeof form[e] == "object") {
if (checkJson(form[e])) form[e] = JSON.stringify(form[e]).replace(/"'/g, "\'") if (checkJson(form[e])) {
else form[e] = JSON.stringify(form[e]) form[e] = JSON.stringify(form[e]).replace(/["']/g, "\\'")
console.log(form[e])
} else form[e] = JSON.stringify(form[e])
} }
return `${e}='${form[e]}'` return `${e}='${form[e]}'`
}) })
@@ -37,7 +39,7 @@ const insert = ({table, form}) => {
if (form[e]) { if (form[e]) {
cols.push(e) cols.push(e)
if (typeof form[e] == "object") { if (typeof form[e] == "object") {
if (checkJson(form[e])) form[e] = JSON.stringify(form[e]).replace(/"'/g, "\'") if (checkJson(form[e])) form[e] = JSON.stringify(form[e]).replace(/["']/g, "\\'")
else form[e] = JSON.stringify(form[e]) else form[e] = JSON.stringify(form[e])
} }
arr.push(`'${form[e]}'`) arr.push(`'${form[e]}'`)