JSON.parse()和JSON.stringify()用法

简介: JSON.parse()和JSON.stringify()用法

JSON.parse() 方法用于将 JSON 格式的字符串转换为 JavaScript 对象,而 JSON.stringify() 方法用于将 JavaScript 对象转换为 JSON 字符串。这两个方法可以组合使用来实现将数据从对象到字符串再到对象的转换。

示例
// 创建一个包含属性的 JavaScript 对象
var person = {
  name: "Alice",
  age: 25,
  city: "New York"
};
 
// 将 JavaScript 对象转换为 JSON 字符串
var jsonString = JSON.stringify(person);
console.log(jsonString); // 输出:{"name":"Alice","age":25,"city":"New York"}
 
// 将 JSON 字符串转换为 JavaScript 对象
var parsedData = JSON.parse(jsonString);
console.log(parsedData.name); // 输出:Alice
console.log(parsedData.age);  // 输出:25
console.log(parsedData.city); // 输出:New York
目录
打赏
0
1
1
0
6
分享
相关文章
|
11月前
|
C++ JSON库 nlohmann::basic_json::boolean_t 的用法
C++ JSON库 nlohmann::basic_json::boolean_t 的用法
204 0
Uncaught SyntaxError: JSON.parse: expected property name or '}' at line 1 column 14 of the JSON data问题如何处理
【6月更文挑战第15天】Uncaught SyntaxError: JSON.parse: expected property name or '}' at line 1 column 14 of the JSON data问题如何处理
370 5
Uncaught SyntaxError: JSON.parse: expected property name or '}' at line 1 column 14 of the JSON data问题处理
【5月更文挑战第14天】Uncaught SyntaxError: JSON.parse: expected property name or '}' at line 1 column 14 of the JSON data问题处理
313 0
vue写入json数据到文本中+vue引入cdn的用法
vue写入json数据到文本中+vue引入cdn的用法
93 10
JSON用法
JSON用法
66 4
|
9月前
|
js【详解】深拷贝 (含 JSON.parse(JSON.stringify(obj)) 的缺陷,5种手写深拷贝)
js【详解】深拷贝 (含 JSON.parse(JSON.stringify(obj)) 的缺陷,5种手写深拷贝)
264 0
|
11月前
|
JSON.stringify()和JSON.parse()
JSON.stringify()和JSON.parse()
124 1
|
11月前
|
C++ JSON库 nlohmann::basic_json::binary_t的用法
C++ JSON库 nlohmann::basic_json::binary_t的用法
176 0
淘宝商品详情API的调用流程(python请求示例以及json数据示例返回参考)
JSON数据示例:需要提供一个结构化的示例,展示商品详情可能包含的字段,如商品标题、价格、库存、描述、图片链接、卖家信息等。考虑到稳定性,示例应基于淘宝开放平台的标准响应格式。