如何使用 Javascript 将数据写入 JSON 文件 [英] How to write data to a JSON file using Javascript

查看:464
本文介绍了如何使用 Javascript 将数据写入 JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个 .JSON 文件,其中包含以下内容:

For example, I have a .JSON file that has the following:

[{"honda": "accord", "color": "red"},{"ford": "focus", "color": "black"}]

将另一个对象 {"nissan": "sentra", "color": "green"} 推送到这个 .json 数组中的 javascript 代码是什么?使 .json 文件看起来像

What would be the javascript code to push another object {"nissan": "sentra", "color": "green"} into this .json array to make the .json file look like

[{"honda": "accord", "color": "red"},{"ford": "focus", "color": "black"},{"nissan": "sentra", "color": "green"}]

我问的原因是我在网上找到了很多关于如何使用 AJAX 从 .json 文件中提取数据但没有使用 AJAX 将新数据写入 .json 文件以更新 .json 文件的信息数据.

The reason I'm asking is I am finding a lot of information online on how to pull data from a .json file using AJAX but not writing new data to the .json file using AJAX to update the .json file with additional data.

任何帮助将不胜感激!

推荐答案

您必须清楚JSON"的含义.

You have to be clear on what you mean by "JSON".

有些人错误地使用术语 JSON 来指代普通的旧 JavaScript 对象,例如 [{a: 1}].这恰好是一个数组.如果你想向数组中添加一个新元素,只需 push 它,如

Some people use the term JSON incorrectly to refer to a plain old JavaScript object, such as [{a: 1}]. This one happens to be an array. If you want to add a new element to the array, just push it, as in

var arr = [{a: 1}];
arr.push({b: 2});

< [{a: 1}, {b: 2}]

JSON 一词也可用于指代以 JSON 格式编码的字符串:

The word JSON may also be used to refer to a string which is encoded in JSON format:

var json = '[{"a": 1}]';

注意(单)引号表示这是一个字符串.如果您有这样的字符串是从某处获得的,则需要首先使用 JSON.parse 将其解析为 JavaScript 对象:

Note the (single) quotation marks indicating that this is a string. If you have such a string that you obtained from somewhere, you need to first parse it into a JavaScript object, using JSON.parse:

var obj = JSON.parse(json);

现在您可以以任何方式操作对象,包括如上所示的 push.如果你想把它放回一个 JSON 字符串,那么你使用 JSON.stringify:

Now you can manipulate the object any way you want, including push as shown above. If you then want to put it back into a JSON string, then you use JSON.stringify:

var new_json = JSON.stringify(obj.push({b: 2}));
'[{"a": 1}, {"b": 1}]'

JSON 还用作格式化数据的常用方法,以便将数据传输到服务器或从服务器传输数据,并在服务器上保存(持久化)数据.这就是 ajax 的用武之地.Ajax 用于从服务器获取数据(通常采用 JSON 格式),和/或将 JSON 格式的数据发送到服务器.如果您收到来自 JSON 格式的 ajax 请求的响应,您可能需要如上所述 JSON.parse 它.然后你可以操作这个对象,用JSON.stringify把它放回JSON格式,并使用另一个ajax调用将数据发送到服务器进行存储或其他操作.

JSON is also used as a common way to format data for transmission of data to and from a server, where it can be saved (persisted). This is where ajax comes in. Ajax is used both to obtain data, often in JSON format, from a server, and/or to send data in JSON format up to to the server. If you received a response from an ajax request which is JSON format, you may need to JSON.parse it as described above. Then you can manipulate the object, put it back into JSON format with JSON.stringify, and use another ajax call to send the data to the server for storage or other manipulation.

您使用术语JSON 文件".通常,文件"一词用于指代某个设备上的物理文件(不是您在代码中处理的字符串或 JavaScript 对象).浏览器无法访问您机器上的物理文件.它无法读取或写入它们.实际上,浏览器甚至没有真正的文件"的概念.因此,您不能只是在本地机器上读取或写入一些 JSON 文件.如果您正在向服务器发送 JSON 和从服务器发送 JSON,那么当然,服务器可能会将 JSON 存储为文件,但更有可能的是,服务器会根据从数据库中检索到的数据,基于某些 ajax 请求构建 JSON,或者在一些ajax请求中解码JSON,然后将相关数据存储回其数据库.

You use the term "JSON file". Normally, the word "file" is used to refer to a physical file on some device (not a string you are dealing with in your code, or a JavaScript object). The browser has no access to physical files on your machine. It cannot read or write them. Actually, the browser does not even really have the notion of a "file". Thus, you cannot just read or write some JSON file on your local machine. If you are sending JSON to and from a server, then of course, the server might be storing the JSON as a file, but more likely the server would be constructing the JSON based on some ajax request, based on data it retrieves from a database, or decoding the JSON in some ajax request, and then storing the relevant data back into its database.

您真的有JSON 文件"吗?如果有,它存在于何处以及从何处获取?您是否有一个 JSON 格式的字符串,需要对其进行解析、处理并转换回新的 JSON 格式字符串?您是否需要从服务器获取 JSON,并对其进行修改,然后将其发送回服务器?或者您的JSON 文件"实际上只是一个 JavaScript 对象,您只需要使用普通的 JavaScript 逻辑对其进行操作?

Do you really have a "JSON file", and if so, where does it exist and where did you get it from? Do you have a JSON-format string, that you need to parse, mainpulate, and turn back into a new JSON-format string? Do you need to get JSON from the server, and modify it and then send it back to the server? Or is your "JSON file" actually just a JavaScript object, that you simply need to manipulate with normal JavaScript logic?

这篇关于如何使用 Javascript 将数据写入 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆