(1)对象的类型转换成字符串类型(或者更确切的说是json类型的)
JSONObject jo = JSONObject.fromObject(map);
常见的java代码转换成json
比如:后台Java有content这个参数
JSONObject jsonObj = JSONObject.fromObject(content);
final String userId = jsonObj.getString("userId");//获取用户编码
(2)
stringify() 把Javascript对象序列化为JSON字符串
parse() 把JSON字符串解析为原生Javascript对象
测试case::::
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
<title>关于我们</title>
<script type="text/javascript">
function init()
{
var book={
title:"Javascript高级程序设计",
authors:[
"Nicholas C. Zakas"
],
edition:3,
year:2011
};
var jsonBook=JSON.stringify(book);
alert(jsonBook);
var objectBook=JSON.parse(jsonBook);
alert(objectBook);
var title=objectBook.title;
alert(title);
}
</script>
</head>
<body>
<input type="button" onclick="init()" value="测试" />
<body >
</body>
</html>