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

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

问题描述

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

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

推送另一个对象的javascript代码是什么 {nissan:sentra,color:green } 进入此 .json 数组,使 .json 文件看起来像

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

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



任何帮助将不胜感激!谢谢。

解决方案

你必须清楚JSON是什么意思。



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

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

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

单词JSON也可用于引用以JSON格式编码的字符串:

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

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

  var obj = JSON.parse(json); 

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

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

JSON还用作格式化数据的常用方法,用于与服务器之间传输数据,可以保存(保存)数据。这就是ajax的用武之地.Ajax用于从服务器获取数据(通常是JSON格式),和/或以JSON格式发送数据到服务器。如果您收到来自jj格式的ajax请求的响应,则可能需要 JSON.parse ,如上所述。然后,您可以操作该对象,使用 JSON.stringify 将其重新置换为JSON格式,并使用另一个ajax调用将数据发送到服务器进行存储或其他操作。 / p>

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



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


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

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

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"}]

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.

Any help would be appreciated! Thanks.

解决方案

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

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}]

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

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

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);

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 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.

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.

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天全站免登陆