当前位置:网站首页>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
边栏推荐
猜你喜欢
帆软中需要设置合计值为0时,一整行都不显示的解决办法
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
多云数据流转?云上容灾?年前最后的价值内容分享
Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
01-NIO基础之ByteBuffer和FileChannel
VMware Workstation 无法连接到虚拟机。系统找不到指定的文件
使用DialogFragment的一些感受及防踩坑经验(getActivity、getDialog为空,cancelable无效等)
Check in system based on ibeacons
redis数据库讲解二(redis高可用、持久化、性能管理)
Jira截取全图
随机推荐
Research on recyclerview details - Discussion and repair of recyclerview click dislocation
Promtail + Loki + Grafana 日志监控系统搭建
MySQL数据库讲解(九)
室内外地图切换(室内基于ibeacons三点定位)
redis数据库讲解二(redis高可用、持久化、性能管理)
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
postman批量生产body信息(实现批量修改数据)
不同时间类型的执行计划计算
Prediction of tomorrow's trading limit of Low Frequency Quantization
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移
RecyclerView高级使用(一)-侧滑删除的简单实现
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
RobotFramework 之 项目框架
数据库DbVisualizer Pro报文件错误,导致数据连接失败
Wechat applet obtains login user information, openid and access_ token
帆软之单元格部分字体变颜色
Use of WiFi module based on wechat applet
报表FCRA考试题集及答案(错了11题)
什么是云迁移?云迁移的四种模式分别是?
On September 8, the night before going to Songshan Lake