1.首先准备好json格式的文件,如图:

2.再创建一个js文件, 如图: 

 3.分析代码思路 :

3.1 读取data.json文件

3.2 将字符串数据转变成数组格式,添加完成数据再转回json格式

3.3 将json格式的数据写入json文件当中

4.代码实现:

1 读取data.json文件

// 导入
const fs = require('fs')
const path = require('path')
//1.读取data.json文件
fs.readFile(path.join(__dirname, 'data.json'), 'utf8', (err, data) => {
  if (err) {
    console.log(err)
    return
  }
  console.log(data);
})

这是我们读取的json数据

Microsoft Windows [版本 10.0.18363.1556]
(c) 2019 Microsoft Corporation。保留所有权利。

D:\桌面\01\Node\nodejs\json>node myfile.js
[
  {
    "name": "小王"
  },
  {
    "name": "小李"
  },
  {
    "name": "小郑"
  },
  {
    "name": "小强"
  }
]

D:\桌面\01\Node\nodejs\json>

 2 将对应的数据进行操作

// 导入
const fs = require('fs')
const path = require('path')
//1.读取data.json文件
let dir = path.join(__dirname, 'data.json')
fs.readFile(dir, 'utf8', (err, data) => {
  if (err) {
    console.log(err)
    return
  }
  console.log(data);
  //2.将字符串数据转变成数组格式
  let arr = JSON.parse(data)
  // console.log(arr);
  let obj = { "name": "小5" }
  //2.1.给数组添加元素
  arr.push(obj)
  let newArr = JSON.stringify(arr)//将数组转成json格式
  // console.log(newArr);
  //3.写入需要的文件当中
  fs.writeFile(dir, newArr, 'utf8', (err) => {
     console.log('写入成功', err)
  })
})

这是我们输出的结果

D:\桌面\01\Node\nodejs\json>node myfile.js
[{"name":"小王"},{"name":"小李"},{"name":"小郑"},{"name":"小强"},{"name":"小5"}]
写入成功 null

D:\桌面\01\Node\nodejs\json>

3.再来看看json文件,这样就数据写入成功了

 到这里我们的读写数据就算基本完成了,这可能是最简单,最基本的,之后我们大部分用到的都是api,然后用post、get、put、等方法对数据进行处理,但练习一下这个也是非常必要的。

补充:
JSON.parse()将json对象转成js数据;
JSON.stringify()将对象、数组转换成json对象。

 今天就先分享到这里,喜欢的朋友可以留言讨论,欢迎点赞加关注,一起学习,一起进步!!!

GitHub 加速计划 / js / json
19
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:4 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 1 个月前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 1 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐