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

时间:2020-11-27 19:05:30



一、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);

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


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

// var json = JSON.stringify(student);
alert(student);
JS对象与JSON格式数据相互转换