We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
语法:JSON.stringify(value[,replacer[, space]])
JSON.stringify(value[,replacer[, space]])
传入一个JSON格式的JS对象或数组:
// 对象 JSON.stringify({"name": "Good Man", "age": 18}) // '{"name": "Good Man", "age": 18}' // 数组 JSON.stringify([1, 2, 3, "4"]) // '[1, 2, 3, "4"]'
var friend = { "firstName": "Good", "lastName": "Man", "phone": "1234567", "age": 18 } var friendAfter = JSON.stringify(friend, function (key, value) { if (key === 'phone') { return "(000)" + value; } else if (typeof value === 'number') { return 10 + value; } else { return value; // 如果你把这个else分句(即是缺少最后的return value),那么结果会是 undefined } }) console.log(friendAfter); // {"firstName": "Good", "lastName": "Man", "phone": "(000)1234567", "age": 28}
如果上面的传入不是键值对的对象形式,而是数组形式的,比如上面传入的是 var friend = ["Jack", "Rose"], 如果是数组形式,那么 key 是索引值,而 value 是这个数组项,记得要在函数内部返回value,不然会出错。
var friend = ["Jack", "Rose"]
var friend = { "firstName": "Good", "lastName": "Man", "address": "1234567", "age": 18, "phone": {"home": "1234567", "work": "7654321"} } var friendAfter = JSON.stringify(friend, ["firstName", "address", "phone"]) console.log(friendAfter); // {"firstName": "Good", "address": "1234567","phone": {}} // 由于 phone 对象的属性不在数组中,所以没有被序列化出来
The text was updated successfully, but these errors were encountered:
No branches or pull requests
深入理解JSON
将JS数据结构转换为JSON字符串:J SON.stringify
语法:
JSON.stringify(value[,replacer[, space]])
基本使用-一个参数
传入一个JSON格式的JS对象或数组:
第二个参数是一个函数或者是一个数组或者null或者未提供
参考文档
The text was updated successfully, but these errors were encountered: