Files
knowledge-kit/Chapter2 - Web FrontEnd/2.8.md
2020-02-25 17:46:51 +08:00

29 lines
595 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# JSON的一些骚操作
* 将json转换为对象JSON.parse\(\)函数的第二个参数用来转换解析出的属性
```
JSON.parse('{"name":"lbp","age":"20"}',function(key,value){
if(key == "name"){
return value + "同学";
}
return value;
});
```
* 将对象转换为jsonJSON.stringify\(\)函数的第二个参数用来筛选对象的键值
```
var student = {"name":"小米","age":22,"height":177,"skills":["js","oc"]};
function convert(key,value){
if (typeof value === "string") {
return value.toString().toUpperCase();
}
return value;
}
JSON.stringify(student,convert,' ');
```