码迷,mamicode.com
首页 > Web开发 > 详细

GSON JSON

时间:2016-05-07 22:10:31      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

实体类Javabean必须要有无参构造,否则报错

 

String s = req.getParameter("pakage");

s = URLDecoder.decode(s,"utf-8");
System.out.println(s);



JSONArray jsonArray = JSONArray.fromObject(s);

List list = (List) jsonArray.toCollection(jsonArray,User.class);

Iterator it = list.iterator();

while(it.hasNext())
{
User user = (User) it.next();
System.out.println(user.getName());
}

--------------------------------------------------------

//这种好用

String s = req.getParameter("pakage");
s = URLDecoder.decode(s,"utf-8");
System.out.println(s);
JSONArray jsonArray = JSONArray.fromObject(s);    //将字符串 专程JSON数组
ArrayList<User> list = new ArrayList<User>();

for (int i = 0; i < jsonArray.size(); i++) {
                            数组遍历

JSONObject jsonObject = (JSONObject) jsonArray.get(i);  这两行就是单个对象转换

list.add((User)JSONObject.toBean(jsonObject, User.class));

}

 

GSON JSON

标签:

原文地址:http://www.cnblogs.com/noteofmine/p/5469147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!