JSON.stringify、JSON.parse、表单序列化

1、JSON.stringify()

  • JSON.stringify(value)
var data = [
    {name: "1", sex:1, age: 1},
    {name: "2", sex:0, age: 2},
    {name: "3", sex:1, age: 3}
];
console.log(JSON.stringify(data));
[{"name":"1","sex":1,"age":1},{"name":"2","sex":0,"age":2},{"name":"3","sex":1,"age":3,"info":{"sex":"male"}}]
  • JSON.stringify(value [, replacer])
 var data = [
    {name: "1", sex:1, age: 1},
    {name: "2", sex:0, age: 2},
    {name: "3", sex:1, age: 3}
];
console.log( JSON.stringify(data, ["name", "sex"]));
 [{"name":"1","sex":1},{"name":"2","sex":0},{"name":"3","sex":1}]
 var data = [
    {name: "1", sex:1, age: 1},
    {name: "2", sex:0, age: 2},
    {name: "3", sex:1, age: 3}
];
var str_json = JSON.stringify(data, function (k, v) {
    if (k === "sex") {
        return ["女", "男"][v];
    }
    return v;
});//sex 0,1->女,男
console.log(str_json);
 [{"name":"1","sex":"男","age":1},{"name":"2","sex":"女","age":2},{"name":"3","sex":"男","age":3,"info":{}}]
  • JSON.stringify(value [, replacer] [, space])
var data = [
    {name: "1", sex:1, age: 1},
    {name: "2", sex:0, age: 2},
    {name: "3", sex:1, age: 3,info:{sex:'male',getSex:function(){return 'sex';}}}
];

var censor = function(key,value){
    if(typeof(value) == 'function'){
         return Function.prototype.toString.call(value)
    }
    return value;
}
console.log(JSON.stringify(data,censor,4))
[
    {
        "name": "1",
        "sex": 1,
        "age": 1
    },
    {
        "name": "2",
        "sex": 0,
        "age": 2
    },
    {
        "name": "3",
        "sex": 1,
        "age": 3,
        "info": {
            "sex": "male",
            "getSex": "function (){return 'sex';}"
        }
    }
]

2、JSON.parse()

  • JSON.parse(text)
var data = '[{"name":"1","sex":1,"age":1},{"name":"2","sex":0,"age":2},{"name":"3","sex":1,"age":3}]';
JSON.parse(data);
  • JSON.parse(text[, reviver])
var data = '[{"name":"1","sex":1,"age":1},{"name":"2","sex":0,"age":2},{"name":"3","sex":1,"age":3}]';
var str_json = JSON.stringify(JSON.parse(data), function (k, v) {
    if (k === "sex") {
        return ["女", "男"][v];
    }
    return v;
});//sex 0,1->女,男
JSON.parse(str_json);

3、jquery操作json

  • serialize()方法
格式:var data = $("#formID").serialize();
功能:将表单内容序列化成一个以&拼接的字符串,键值对的形式,name1=val1&name2=val2&,空格以%20替换。
  • serializeArray()方法
格式:var jsonData = $("#formID").serializeArray();
功能:将页面表单序列化成一个JSON结构的对象。注意不是JSON字符串。
  • param()方法
格式:var string = $("#formID").param();
功能:可以把json格式数据序列化成字符串形式

var obj={a:1,b:2};
$.param(obj);
"a=1&b=2"
  • 自定义方法serializeJson()
格式:var jsonData = $("#formID").serializeJson();
功能:serializeArray()方法的扩展,格式转换为键值对name:val。

$.fn.serializeJson= function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function(){
        if(o[this.name]){
            if(!o[this.name].push){
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value ||'');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值