生活随笔
收集整理的這篇文章主要介紹了
JavaScript | JSON基本格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
—————————————————————————————————————————————————————————
JSON
語法
"use strict"
;
// 簡單值
"hello,world"
// 必須使用雙引號// 對象
{"name": "hugh"
,"age": 12
,"school"
:{"name": "xzcit"
,"location": "North Andover,MA"
}
}// 數(shù)組
[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方法并且能通過它取得有效的值,則調(diào)用該方法。否則返回對象本身
- 如果提供了第二個參數(shù),應(yīng)用這個函數(shù)過濾器,傳入函數(shù)過濾器的值是步驟1返回的值
- 對步驟2返回的值進(jìn)行相應(yīng)的序列化
- 如果提供了第三個參數(shù),執(zhí)行相應(yīng)的格式化
"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// 過濾結(jié)果
var jsonText = JSON.stringify(book, ["title", "year"]);
// 過濾只保留title和year
console.log(jsonText);// 修改返回結(jié)果
var jsonText = JSON.stringify(book,
function(key, value) {
// 傳入鍵值對switch (key) {case "author"
:return value.join("||");
// 將數(shù)組連接成為字符串case "year"
:return 5000
;case "edition"
:return undefined;
// 返回undefined屬性被忽略default:return value;
// 其他返回本身值
}
});
console.log(jsonText);// 字符串縮進(jìn)
var jsonText = JSON.stringify(book,
null, 4);
// 縮進(jìn)4個空格,最大縮進(jìn)10格
console.log(jsonText);
var jsonText = JSON.stringify(book,
null, " - -");
// 特殊符號縮進(jìn)
console.log(jsonText);// toJSON()方法
var book =
[{"title": "Professional JavaScript1"
,"author"
: ["Nicholas C.Zakas"
,"hugh"
],"edition": 3
,"year": 2002
,"toJSON":
function() {
// toJSON可以作為函數(shù)過濾器的補(bǔ)充return this.title;}
}];
var jsonText =
JSON.stringify(book);
console.log(jsonText); ?
解析JSON對象
- 早起JSON解析器是使用eval()函數(shù),但eval()可能會執(zhí)行惡意代碼
- 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()); ?
?
轉(zhuǎn)載于:https://www.cnblogs.com/hughdong/p/7265216.html
總結(jié)
以上是生活随笔為你收集整理的JavaScript | JSON基本格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。