Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js json序列化

作者:月下孤魂   发布日期:2025-12-08   浏览:61

// JSON序列化示例代码

// 定义一个JavaScript对象
const person = {
    name: "Alice",
    age: 25,
    isStudent: false,
    hobbies: ["reading", "swimming"],
    address: {
        city: "Beijing",
        country: "China"
    }
};

// 使用JSON.stringify()方法将JavaScript对象转换为JSON字符串
const jsonString = JSON.stringify(person);
console.log(jsonString); 
// 输出: {"name":"Alice","age":25,"isStudent":false,"hobbies":["reading","swimming"],"address":{"city":"Beijing","country":"China"}}

// 如果需要更易读的格式,可以传递额外的参数给JSON.stringify()
const prettyJsonString = JSON.stringify(person, null, 4);
console.log(prettyJsonString);
// 输出:
// {
//     "name": "Alice",
//     "age": 25,
//     "isStudent": false,
//     "hobbies": [
//         "reading",
//         "swimming"
//     ],
//     "address": {
//         "city": "Beijing",
//         "country": "China"
//     }
// }

上一篇:js 字符串比较大小

下一篇:js blob转base64

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站