JS中JSON对象和字符串的相互转化
程序员文章站
2022-05-23 22:14:02
...
var obj1 = { "name": "Lancelot", "age":10, "address": "云南" }; var obj2 = { name : "Lancelot", age : 10, address : "云南" }; //访问属性 console.info("name = " + obj1.name + " , age = " + obj1.age + " , type = " + typeof obj1); console.info("name = " + obj2.name + " , age = " + obj2.age + " , type = " + typeof obj2); //输出JSON对象 console.info(obj1); console.info(obj2); //输出Object对象 console.info(obj1.toString()); console.info(obj2.toString()); //JSON对象输出JSON字符串:JSON.stringify() console.info(JSON.stringify(obj1)); console.info(JSON.stringify(obj2)); var obj1Str = JSON.stringify(obj1); var obj2Str = JSON.stringify(obj2); //JSON字符串转化为JSON对象:JSON.parse() console.info(JSON.parse(obj1Str)); console.info(JSON.parse(obj2Str)); //JSON字符串转化为JSON对象:string.parseJSON() //console.info(obj1Str.parseJSON()); //console.info(obj2Str.parseJSON()); //JSON字符串转化为JSON对象:eval()方法 console.info(eval('('+obj1Str+')')); console.info(eval('('+obj2Str+')'));
上一篇: 在C#中对枚举进行位运算--枚举组合
下一篇: 代码坏味道