对象. 创建 (“ScriptControl”, )对象. 写属性 (“Language”, “javascript”)对象. 方法 (“AddCode”, #jsCode )输出调试文本 (对象. 通用方法 (“run”, “test”, ). 取文本 ())
其中 #jsCode为一个常量,里面放着js代码
test为js代码里的函数名。
<html>
<head>
<meta charset="UTF-8">
<title>js解析json</title>
</head>
<body>
<script type="text/javascript">
function showJSON() {
var user = {
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"address": [
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}
alert("user.username = " + user.username);
alert("user.age = " + user.age);
alert("user.info.cellphone = " + user.info.cellphone);
alert("user.address[0].city = " + user.address[0].city);
alert("user.address[0].postcode = " + user.address[0].postcode);
user.username = "Tom";
alert("user.username = " + user.username);
}
</script>
<input type="button" value="showJSON" />
</body>
</html>
上面的的代码,可以放在浏览器看看效果。
json格式的字符串转成js的json对象
var jsonObj = eval("(" + jsonStr +")");
|