当前位置:网站首页>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
边栏推荐
- 帆软中单元格中隔行变色以及数量大于100字体变大变红设置
- 什么是云迁移?云迁移的四种模式分别是?
- 在MAC上安装mysql
- VMWare安装64位XP中文教程
- RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复
- Research on recyclerview details - Discussion and repair of recyclerview click dislocation
- 帆软之单元格部分字体变颜色
- Operation instructions of star boundary text automatic translator
- Idea控制台乱码解决
- org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
猜你喜欢
CDH cluster integration Phoenix based on CM management
基于CM管理的CDH集群集成Phoenix
连接公司跳板机取别名
Visio installation error 1:1935 2: {XXXXXXXX
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
Check in system based on ibeacons
使用Postman进行Mock测试
Subscription number development of wechat applet (message push)
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
容灾有疑问?点这里
随机推荐
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
云容灾是什么意思?云容灾和传统容灾的区别?
不同时间类型的执行计划计算
关于训练过程中损失函数出现断崖式增长的问题
云迁移的六大场景
gzip和gunzip 解压参数详解
Check in system based on ibeacons
JSP学习2
微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
squid代理
mysql 5.1升级到5.69
快速安装mongodb
Nifi 快速安装及文件同步操作
Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)
FBS (fman build system) packaging
RecyclerView细节研究-RecyclerView点击错位问题的探讨与修复
星界边境文本自动翻译机使用说明
封装logging模块