JS对象与JSON格式数据相互转换



一、JS对象转换成为JSON 

var   jsonData = {};
jsonData.MainCmdID = 1;
jsonData.SubCmdID = 1;
var toStr = JSON.stringify(jsonData);
alert(toStr);


二、JSON转换成为JS

var result2 = $.parseJSON( '{ "name": "CodePlayer", "age": 1 }' ); // 一个Object对象
alert( result2.name ); // CodePlayer







例:

var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);
alert(student);



var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";

// var json = JSON.stringify(student);
alert(student);










你可能感兴趣的:(JS对象与JSON格式数据相互转换)