当前位置:网站首页>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
边栏推荐
- 教育行业云迁移最佳实践:海云捷迅使用HyperMotion云迁移产品为北京某大学实施渐进式迁移,成功率100%
- 什么是云迁移?云迁移的四种模式分别是?
- Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
- Detailed tutorial on the use of setinterval timing function of wechat applet
- RobotFramework 之 公共变量
- pthread_ Why does self() repeat
- VMware 15pro mounts the hard disk of the real computer in the deepin system
- rsync+inotify远程同步
- HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
- Switch usage (wechat applet)
猜你喜欢

Gartner预测云迁移规模大幅增长;云迁移的优势是什么?

DeepinV20安装Mariadb
POI operation word template replaces data and exports word

倒计时1天~2022云容灾产品线上发布会即将开始

On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication

MySQL数据库讲解(十)

Intégration de Clusters CDH Phoenix basée sur la gestion cm

RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复

Chrome插件 之 Selenium IDE、XPath 安装

基於CM管理的CDH集群集成Phoenix
随机推荐
Three point positioning based on ibeacons (wechat applet)
redis数据库讲解(三)redis数据类型
sql中出现一个变态问题
squid代理
Idea控制台乱码解决
Understand the concepts of virtual base class, virtual function and pure virtual function (turn)
按实际取,每三级分类汇总一次,看图知需求
室内外地图切换(室内基于ibeacons三点定位)
Algorithem_ReverseLinkedList
数据库DbVisualizer Pro报文件错误,导致数据连接失败
RecyclerView高级使用(二)-垂直拖拽排序的简单实现
HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
修改ddt生成的测试用例名称
RobotFramework 之 用例执行
Chrome插件 之 Selenium IDE、XPath 安装
帆软报表设置单元格填报以及根据值的大小进行排名方法
基於CM管理的CDH集群集成Phoenix
Wechat applet input hidden and inoperable settings
Oracle-数据泵使用
回顾2021:如何帮助客户扫清上云最后一公里的障碍?