当前位置:网站首页>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
边栏推荐
- 服务器日志分析工具(识别,提取,合并,统计异常信息)
- On September 8, the night before going to Songshan Lake
- 如何快速批量创建文本文档?
- Essential difference between restful WebService and gSOAP webservice
- 使用Postman进行Mock测试
- 多云数据流转?云上容灾?年前最后的价值内容分享
- 不同时间类型的执行计划计算
- 关于NodeJS中JSON5的相关配置和使用
- HyperBDR云容灾V3.2.1版本发布|支持更多云平台,新增监控告警功能
- Multiple inheritance virtual base exercises
猜你喜欢
随机推荐
RobotFramework 之 公共变量
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
Oracle-数据泵使用
基于CM管理的CDH集群集成Phoenix
Switch usage (wechat applet)
帆软中单元格中隔行变色以及数量大于100字体变大变红设置
在MAC上安装mysql
微信小程序setInterval定时函数使用详细教程
redis数据库讲解(四)主从复制、哨兵、Cluster群集
云迁移的六大场景
Mock测试
使用DialogFragment的一些感受及防踩坑经验(getActivity、getDialog为空,cancelable无效等)
VMWare安装64位XP中文教程
RobotFramework 之 项目框架
Chapter I review of e-commerce spike products
POI operation word template replaces data and exports word
Understand the concepts of virtual base class, virtual function and pure virtual function (turn)
使用开源调研工具Prophet是一种什么体验?
Homebrew是什么?以及使用
Jmeter设置环境变量支持在任意终端目录输入jmeter直接启动