http://topcss.iteye.com/blog/1069680
http://heisetoufa.iteye.com/blog/1882776
首先下载一个jar包:
下载地址:http://code.alibabatech.com/svn/fastjson/repository/com/alibaba/fastjson/1.0.4/fastjson-1.0.4.jar
导入到你的lib目录下。
在你的代码中这样用:
- public static void main(String[] args) {
- User_DAL userDal = new User_DAL();// 创建数据访问对象
- ResponseResult<User> ret = userDal.getUserList("id < 50", 2, 10);// 查询ID小于50的用户,保存到分页类中
- String json = JSON.toJSONString(ret, true); // 将对象转换成json字符串,true为格式化输出
- System.out.println(json);
- List<User> list = JSON.parseArray("{{name:123},{name:456}}", User.class);//将json字符串,转换成Java对象数组
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i).getName());
- }
- }
.首先要引入一个js文件,否则一些浏览器上用不了
json2.js
可以在https://github.com/douglascrockford/JSON-js 上下载
- var a={"name":"heisetoufa","sex":"男","age":"29"};
- var b='{"name":"zisetoufa","sex":"女","age":"24"}';
- var aToStr=JSON.stringify(a);
- var bToObj=JSON.parse(b);
- alert(typeof(aToStr));//string
- alert(typeof(bToObj));//object