JSON5 对 JSON 进行了扩展,增加了如下功能:
使用 Node.js 环境,通过一个简单示例,展示JSON5的使用方式以及新的功能。Node.js的安装,看这里!
cd learn-json5
npm install json5
const JSON5 = require('json5')
const obj = {
// comments
/*
multi
comments
*/
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}
const res = JSON5.stringify(obj)
console.log(res)
console.log("***************************");
console.log(JSON5.parse(res))
/* 输出结果如下:
{unquoted:'and you can quote me on that',singleQuotes:'I can use "double quotes" here',lineBreaks:"Look, Mom! No \\n's!",hexadecimal:912559,leadingDecimalPoint:0.8675309,andTrailing:8675309,positiveSign:1,trailingComma:'in objects',andIn:['arrays'],backwardsCompatible:'with JSON'}
***************************
{
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! No \\n's!",
hexadecimal: 912559,
leadingDecimalPoint: 0.8675309,
andTrailing: 8675309,
positiveSign: 1,
trailingComma: 'in objects',
andIn: [ 'arrays' ],
backwardsCompatible: 'with JSON'
}
*/
node test.js
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。