当前位置:网站首页>About the configuration and use of json5 in nodejs
About the configuration and use of json5 in nodejs
2022-04-23 14:11:00 【Senzhiqianshou】
Preface
JSON5 yes JSON An extended subset of . Therefore, it is fully compatible with old JSON, And it has its own unique characteristics :
- Support comments
- Double quotation marks are not required , You can use single quotes
- The key of a key value pair can be enclosed without double quotation marks
- Allow the last element to be followed by a comma
for instance , natural json It looks like this :
[
{
"name": "Frank",
"age": 16,
"gender": "male",
"isAdult": false
},
{
"name": "Bob",
"age": 22,
"gender": "male",
"isAdult": true
}
]
Use json5 It became like this :
/* This is a json5 test */
[
{
name: "Frank",// full name
age: 16,// Age
gender: "male",
isAdult: false
},
{
name: "Bob",
age: 22,
gender: "male",
isAdult: true
},
]
This greatly expands JSON Readability .
How to use
Now let's talk about how to work in a NodeJS Used in the project JSON5.
1、 install JSON5 Dependence
npm install json5 --save
2、 New suffix json5 The file of , Let's build a new one ‘tt.json5’

Fill in the above
3、 Read
We installed json5 In fact, there are parse and stringify Two methods , These two methods , about js For the students of development , I can't be more familiar with . In fact, we often use parse This deserialization method .stringify Words , Use your own JSON Can also be realized .
import parse = require('./parse')
import stringify = require('./stringify')
export {
parse, stringify}
nodeJS You can use your own fs Read the file :
const fs=require('fs')
const JSON5 =require('json5')
fs.open('./tt.json5','r+',(err,d)=>{
fs.readFile('./tt.json5','utf-8',(err,data)=>{
console.log(JSON5.parse(data))
})
})
What I read from the file is string, So we call parse Method to deserialize .
If you don't want to use require, Instead, I want to use it import introduce , It's OK .
(1) If node Version of is less than 13
To be precise , Less than 13.2.0 Words , Self installation required babel transcoding , Related configuration reference :
Node.js Use Babel Perfect configuration ES6+
(2) If node The version is greater than 13
Need to be in package.json The inside contains name attribute ( Generally the first ) Of object Increase in
"type": "module"
Otherwise it will be reported :
SyntaxError: Cannot use import statement outside a module
Now you can happily introduce :
import * as fs from 'fs'
import JSON5 from 'json5'
fs.open('./tt.json5','r+',(err,d)=>{
console.log(d)
fs.readFile('./tt.json5','utf-8',(err,data)=>{
console.log(JSON5.parse(data))
})
})
Be careful :
import JSON5 from 'json5'
there JSON5 It's equivalent to you taking an alias , Even if you write AA It's OK .
thus , Usage introduction end .
版权声明
本文为[Senzhiqianshou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231405036787.html
边栏推荐
- Three point positioning based on ibeacons (wechat applet)
- VMware15Pro在Deepin系统里面挂载真机电脑硬盘
- MySQL数据库讲解(九)
- On September 8, the night before going to Songshan Lake
- 封装logging模块
- JDBC详解
- FBS (fman build system) packaging
- 帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
- Wechat applet obtains login user information, openid and access_ token
- 关于云容灾,你需要知道这些
猜你喜欢

微信小程序通过低功耗蓝牙设备进行定位及测距(二)
Check in system based on ibeacons
Jira截取全图

Mock测试

帆软实现分页时第一行和最后两行冻结方式

Postman的安装使用及填坑心得

帆软中根据分类进行汇总

关于云容灾,你需要知道这些

Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in

RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
随机推荐
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
How QT designer adds resource files
Intégration de Clusters CDH Phoenix basée sur la gestion cm
快速安装mongodb
Call wechat customer service applet
帆软中单元格中隔行变色以及数量大于100字体变大变红设置
mysql 5.1升级到5.66
Logging module
星界边境Starbound创意工坊订阅的mod的存放路径
logback-logger和root
关于训练过程中损失函数出现断崖式增长的问题
烟雾传感器(mq-2)使用详细教程(基于树莓派3b+实现)
封装logging模块
FBS (fman build system) packaging
百度图片识别自定义实现(替代AipOcr)
云迁移的六大场景
倒计时1天~2022云容灾产品线上发布会即将开始
DeepinV20安装Mariadb
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
微信小程序通过低功耗蓝牙设备进行定位及测距(二)