JSON详细总结

Posted 给力叔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON详细总结相关的知识,希望对你有一定的参考价值。

/**
 * Created by fa on 2016/3/15.
 */
var data = {
    name:"hello",
    children:[{
        name:"child",
        height:50
    }]
}
console.log(JSON.stringify(data));
//第二个参数是数组就表示只获取指定的属性
console.log(JSON.stringify(data,["children"]));
//第二个参数是function,如果返回值是undefined表示忽略,其它就使用返回值
console.log(JSON.stringify(data,function(key,value){
    switch (key){
        case "name":{
            return undefined;
        }
        case "height":
            return {value:"very height"};
        default :
            return value;
    }
}));
//第三个参数可以把换行符换成自己想要的符号
console.log(JSON.stringify(data,null,"-- -"));
//有toJSON函数就使用toJSON函数的返回值
data = {
    toJSON:function(){
        return {name:"toJSON"}
    }
}
console.log(JSON.stringify(data));

function Persion(){

}
data = {
    name:"Persion",
    msg:"hello"
}
//parse的第二个参数可以设定转换规则
var obj = JSON.parse(JSON.stringify(data),function(key,value){
    if(key === "name"){
        return new Persion();
    }
    return value;
})

 

以上是关于JSON详细总结的主要内容,如果未能解决你的问题,请参考以下文章

json 个人的vscode的代码片段

vbscript 各种自定义代码片段 - 有关详细信息,请参阅注释

实用代码片段将json数据绑定到html元素 (转)

json 可视代码工作室Angular with Firebase片段

vs code 用户代码片段 html.json

Json取值工具类