JavaScript | JSON基本格式

—————————————————————————————————————————————————————————

JSON

语法

"use strict";
// 简单值
"hello,world" // 必须使用双引号

// 对象
{
    "name": "hugh",
    "age": 12,
    "school":
    {
        "name": "xzcit",
        "location": "North Andover,MA"
    }
}

// 数组
[25, "hi", true]

// 组合使用
[
{
    "title": "Professional JavaScript",
    "author": [
        "Nicholas C.Zakas"
    ],
    "edition": 3,
    "year": 2002
},
{
    "title": "Professional JavaScript",
    "author": [
        "Nicholas C.Zakas"
    ],
    "edition": 4,
    "year": 2003
},
{
    "title": "Professional JavaScript",
    "author": [
        "Nicholas C.Zakas"
    ],
    "edition": 5,
    "year": 2004
}]

序列化JSON对象

  • stringify() - 把js对象序列化为json字符串
  • stringify()序列化对象的顺序
    • 如果存在toJSON方法并且能通过它取得有效的值,则调用该方法。否则返回对象本身
    • 如果提供了第二个参数,应用这个函数过滤器,传入函数过滤器的值是步骤1返回的值
    • 对步骤2返回的值进行相应的序列化
    • 如果提供了第三个参数,执行相应的格式化
"use strict";
var book = [{
    "title": "Professional JavaScript1",
    "author": [
        "Nicholas C.Zakas",
        "hugh"
    ],
    "edition": 3,
    "year": 2002
}];
// 使用JSON.stringify()把一个js对象序列化为一个json字符串,保存在变量jsonText中
var jsonText = JSON.stringify(book);
console.log(jsonText);
console.log(typeof jsonText); // string

// 过滤结果
var jsonText = JSON.stringify(book, ["title", "year"]); // 过滤只保留title和year
console.log(jsonText);

// 修改返回结果
var jsonText = JSON.stringify(book, function(key, value) { // 传入键值对
    switch (key) {
        case "author":
            return value.join("||"); // 将数组连接成为字符串
        case "year":
            return 5000;
        case "edition":
            return undefined; // 返回undefined属性被忽略
        default:
            return value; // 其他返回本身值
    }
});
console.log(jsonText);

// 字符串缩进
var jsonText = JSON.stringify(book, null, 4); // 缩进4个空格,最大缩进10格
console.log(jsonText);
var jsonText = JSON.stringify(book, null, " - -"); // 特殊符号缩进
console.log(jsonText);

// toJSON()方法
var book = [{
    "title": "Professional JavaScript1",
    "author": [
        "Nicholas C.Zakas",
        "hugh"
    ],
    "edition": 3,
    "year": 2002,
    "toJSON": function() { // toJSON可以作为函数过滤器的补充
        return this.title;
    }
}];
var jsonText = JSON.stringify(book);
console.log(jsonText);

解析JSON对象

  • 早起JSON解析器是使用eval()函数,但eval()可能会执行恶意代码
  • parse() - 把json字符串解析为原生js
"use strict";
var book = {
    "title": "Professional JavaScript1",
    "author": [
        "Nicholas C.Zakas",
        "hugh"
    ],
    "edition": 3,
    "year": 2002,
    "releaseDate": new Date(2011, 11, 1)
};
var jsonText = JSON.stringify(book);
console.log(jsonText);
var bookCopy = JSON.parse(jsonText, function(key, value) {
    if (key == "releaseDate") {
        return new Date(value);
    } else {
        return value;
    }
})
console.log(bookCopy.releaseDate);
console.log(bookCopy.year);

//使用 JSON.parse 反序列化 ISO 格式的日期字符串, 将返回Date格式对象。  
var jsonText = '{ "hiredate": "2008-01-01T12:00:00Z", "birthdate": "2008-12-25T12:00:00Z" }';
var dates = JSON.parse(jsonText, dateReviver);
function dateReviver(key, value) {
    var a;
    if (typeof value === 'string') {
        a = /^(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2}(?:.d*)?)Z$/.exec(value);
        if (a) {
            return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
        }
    }
    return value;
};
console.log(dates.birthdate.toUTCString());

原文地址:https://www.cnblogs.com/hughdong/p/7265216.html