一、 json字符串转换为javascript对象,并取值
var answer = '{"id":0}' var value= JSON.parse(answer); //转换为json对象 alert(value.id); //取值
二、javascript对象转换为json字符串
沿用例子中value
var json_str= JSON.stringify(value); //转换为json字符串
三、json数组字符串转换为json对象,并取值
var jsonStr = '[ {"id":"01","open":false,"pId":"1","name":"A部门"}, {"id":"02","open":false,"pId":"2","name":"B部门"}, {"id":"03","open":false,"pId":"3","name":"B部门"},
{"id":"04","open":false,"pId":"4","name":"A部门"},
{"id":"05","open":false,"pId":"5","name":"A部门"},
{"id":"06","open":false,"pId":"6","name":"A部门"} ]'; var jsonObj = JSON.parse(jsonStr); //转换为json对象
for(var i=0;i<jsonObj.length;i++){ alert(jsonObj[i].id); //取id }