我收到一个有效的JSON文件,但我想添加"数值周围和布尔值以在wix中继器中发送该JSON;在数字和布尔值周围加倍
如果有人能帮我,请
收到的示例JSON:
[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1}]
需要示例JSON:
[{"id":"1238890","category_id":"1","season_id":"14866","venue_id":"null","referee_id":"null","slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":"0","max":"2700","timestamp":"1660296540","extra":"540"}, "_id":"1"}]
我尝试了下面的代码来转换json,但没有得到想要的结果
我的文章中的代码是一个转换程序,它是一个无需顾问的链字符串。。
const regex = /[^"d,]?(d+)/g;
const str = [{"id":"1238890","category_id":"1","season_id":"14866","venue_id":"null","referee_id":"null","slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":"0","max":"2700","timestamp":"1660296540","extra":"540"}, "_id":"1"}]
const subst = `:` + `"$1"`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
有线
使用replacer
函数解析json,重新字符串(请参阅MDN(。下面的代码段还引用了null
:
const json = JSON.parse(`[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1, "someBoolean":false}]`);
console.log(JSON.stringify(json,
(key, value) =>
/number|boolean/.test(typeof value) || value === null ? `${value}` : value, 2));
.as-console-wrapper {
max-height: 100% !important;
}
我宁愿只转换对象,也不愿使用regex
const json = `[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1}]`
const data = JSON.parse(json)
const converted = convertToStrings(data)
console.log(converted)
function convertToStrings(obj) {
if (['boolean', 'number'].includes(typeof obj)) {
return obj.toString()
}
if (obj === null) {
return 'null'
}
if (typeof obj === 'string') {
return obj
}
if (Array.isArray(obj)) {
const newArray = []
for (const item of obj) {
newArray.push(convertToStrings(item))
}
return newArray
}
if (typeof obj === 'object') {
const newObj = {}
for (const key in obj) {
newObj[key] = convertToStrings(obj[key])
}
return newObj
}
throw new Error('Unsupported property type ' + typeof obj)
}
正如其他人所指出的,将JSON字符串转换为对象、操作对象并转换回JSON字符串是一种很好的方法。
这里有一个简短而甜蜜的正则表达式,可以直接更改JSON字符串:
const jsonStr = '[{"id":1238890,"category_id":1,"season_id":14866,"venue_id":null,"referee_id":null,"slug":"2022-08-12-melbourne-knights-fc-altona-magic-sc","name":"Melbourne Knights FC – Altona Magic SC","status":"inprogress","time_details":{"prefix":"","initial":0,"max":2700,"timestamp":1660296540,"extra":540}, "_id":1, "someBoolean":false}]';
const re = /(":s*)(d+|true|false|null)(,s*"|s*[}]])/g;
console.log(jsonStr.replace(re, '$1"$2"$3'));
正则表达式的解释:
(":s*)
—捕获组1:转义值之前的预期模式(d+|true|false|null)
—捕获组1:要转义的值(,s*"|s*[}]])
—捕获组1:待转义值之后的预期模式/g
—多次更换