把普通对象转换成json格式的对象
var obj = {name:"zhan",age:3;} //=> 普通格式的对象
var jsonObj = {"name":"zhan","age":"3"} //=>键值都加上双引号就变成,json格式的对象
var data = [{name:"zhan",age:3},{name:"zhang",age:4},] //=>普通的二维数组
var jsonData = [{"name":"zhan","age":3},{"name":"zhang","age":4},] //=>json格式的数据
->stringify:把JSON格式/普通格式的对象
转换为JSON格式的字符串
->parse:把JSON格式的字符串
转换为JSON格式的对象
当在进行post请求时,要求请求头部是Content-Type: application/json的时候,请求data参数就需要用JSON.stringify(data)
进行转换才能成功请求
var str= JSON.stringify(data
) data转str
var data =JSON.parse(str
) str转data
var data = [
{name: "zhan", age: 3},
{name: "zhang", age: 4}
];
var str = JSON.stringify(data);//->'[{"name":"zhan","age":3},{"name":"zhang","age":4}]'
console.log(JSON.parse(str));//=>返回原来的普通的二维数组