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

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

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

[{"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.

任何帮助都将不胜感激!

推荐答案

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

请注意表示这是一个字符串的(单)引号.如果您有一个从某处获得的字符串,则需要首先使用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格式的数据.如果您收到来自JSON格式的ajax请求的响应,您可能需要如上所述对其进行修改.然后,您可以操作该对象,使用JSON.stringify将其重新转换为JSON格式,并使用另一个ajax调用将数据发送到服务器进行存储或其他操作.

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?

Json相关问答推荐

如何获取brew list作为JSON输出

将JSON输入子数组转换为字符串顺序列表输出

在Jenkins中使用ReadJSON读取json子元素

在Golang中从 struct 手动创建JSON对象

在MongoDB中检索某个月份和年份的JSON文档

使用JQ从jsonl文件中删除具有匹配键/值的行

使用 jolt 变换展平嵌套 JSON - 非常复杂

如何使用 JOLT 使用输入数组中的值和层次 struct 中的其他字段创建数组

如何使用nifi从json文件中过滤属性

JOLT 在 struct 体中间添加一个 JSON 字段

如果值不存在,则将值插入 JSON 数组

如何判断 Json 对象中是否存在键并获取其值

JSON 语法错误:'unexpected number' 或 'JSON.parse: expected ',' or '}' after property value in object'

JSON对象中的JavaScript递归搜索

将 CoffeeScript 项目转换为 JavaScript(不缩小)?

在 Webpack 中加载静态 JSON 文件

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

将多个值存储在json中的单个键中

FastAPI:如何将正文读取为任何有效的 json?

play 2 JSON 格式中缺少的属性的默认值