person.name="zy";
person.age=30;
person.sex="男";
alert(person.toJSONString());//{"name":"zy","age":30,"sex":"男"}
//需求:添加一个功能 toJSONString(),将属性的值以json格式输出
//{"name":"zy","age":30,"sex":"男"}
//for(var i in person) person[i]取出值
在JSP页面中输出JSON格式数据
JSON-taglib是一套使在JSP页面中输出JSON格式数据的标签库。
JSON-taglib主页: http://json-taglib.sourceforge.net/index.html
JAR包下载地址: http://sourceforge.net/projects/json-taglib/files/latest/downloa......
答案就在这里:在JSP页面中输出JSON格式数据
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
JSON.Stringfy(person);
IE8 以下引入json2.js
JSONArray jsonArr = JSONArray.fromObject(json); // [{"name":"zy","age":30,"sex":"男"},{"name":"zy","age":30,"sex":"男"}]
JSONObject jsonObj = JSONObject.fromObject(jsonArr); // {"name":"zy","age":30,"sex":"男"}
分别对应格式
倒入json2.js,然后自己给object的prototype增加toJSONString方法即可
<script src="http://www.w3dev.cn/rardownload/20130106/20130106170832648.js"></script>
<script>
Object.prototype.toJSONString=function(){return JSON.stringify(this)}
person={}
person.name="zy";
person.age=30;
person.sex="男";
alert(person.toJSONString());//{"name":"zy","age":30,"sex":"男"}
</script>
js原生扩展:
blog地址:http://blog.csdn.net/w172087242/article/details/51899560
下载地址:http://download.csdn.net/detail/w172087242/9575125