javascript - 编辑本地 .json 文件
问题描述
我正在尝试通过我在它旁边创建的 .js 文件来编辑 .json 文件。我正在使用 ajax 调用从我的 .json 文件中获取数据,但对于编辑我不知道该怎么做,这是我获取数据的代码:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
objects =JSON.parse(xhttp.responseText) ;
}
};
xhttp.open("GET", "object.json", true);
xhttp.send();
这也是我的 .json 文件:
{
"circles":[
{ "id":1 ,
"cx":100,
"cy":100,
"r":50,
"stroke":"green" ,
"strokeWidth":4,
"fill":"yellow"
},
{
"id":2 ,
"cx":30,
"cy":30,
"r":10,
"stroke":"green" ,
"strokeWidth":4,
"fill":"yellow"
}
]
}
请给我一个线索谢谢你们:))
解决方案
您可以从前端从服务器获取数据,但要编辑数据,您需要创建一些后端功能。这可以是服务器或无服务器函数,也可以是托管的 db 调用,但您无法在没有后端的情况下编辑纯 .json 文件。
推荐阅读
- javascript - javascript转换成父子数组
- jquery - dayclick不显示模式弹出窗口
- r - 将文本文件保存在 R 包的数据文件夹中
- go - 类型实现中的隐藏方法?
- apache-kafka - 消费者在运行生产者和消费者 python 脚本时没有显示消息
- swift - 使用 userID 写入数据并使用 userID 读取数据
- php - 如何使用 php 标头函数像使用 html 表单一样发送 http post 请求
- javascript - 无法读取未定义的属性“writeHead”
- ios - WKWebView 不允许在应用程序中访问相机
- reactjs - 如何使用 jest 和酵素来测试动态生成的内容?