概述
JSON stringify和字串化工具幫助您在 JSON 字串和 JSON 物件之間進行轉換。它支援將 JSON 字串解析為格式化物件,以及將物件字串化為 JSON 格式。
JSON parse
將 JSON 字串轉換為格式化物件。此模式幫助您驗證和格式化 JSON 字串。
原始 JSON 字串:
{\"name\":\"John Doe\",\"age\":30,\"isStudent\":false,\"hobbies\":[\"reading\",\"gaming\"],\"address\":{\"city\":\"New York\",\"country\":\"USA\"}}格式化的 JSON:
{
"name": "John Doe",
"age": 30,
"isStudent": false,
"hobbies": [
"reading",
"gaming"
],
"address": {
"city": "New York",
"country": "USA"
}
}JSON stringify
將 JSON 字串轉換為格式化物件。此模式幫助您驗證和格式化 JSON 字串。
JSON 物件:
const personData = {
name: "John Doe",
age: 30,
isStudent: false,
hobbies: ["reading", "gaming"],
address: {
city: "New York",
country: "USA"
}
};轉換為 JSON 字串:
// 壓縮的 JSON 字串(帶轉義引號)
const rawJson = "{\"name\":\"John Doe\",\"age\":30,\"isStudent\":false,\"hobbies\":[\"reading\",\"gaming\"],\"address\":{\"city\":\"New York\",\"country\":\"USA\"}}";
// 美化的 JSON 字串
const prettyJson = JSON.stringify(personData, null, 2);選項
- 縮排大小:選擇 2、4 或 8 個空格進行格式化
- 轉義 Unicode:將 Unicode 字元轉換為 \uXXXX 格式
- 移除空白:輸出無空格或換行的壓縮 JSON