暂时以知两种模式
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.15</version>
<scope>compile</scope>
</dependency>
1. Java对象转字符串
String s = JSON.toJSONString(ret);
2.字符串转java对象
String js=”{\”name\”:\”bi”,\”age\”:23}”;
User user = JSON.parseObject(js,User.class);
User user =JSON.parseObject(js,new TypeReference<User>())
3.字符串转换为List
ArrayList<User>users = JSON.parseObject(js,newTypeReference<ArrayList<User>>(){});
4.字符串转化为对象数组
User[] users =JSON.parseObject(js,new TypeReference<User[]>(){});
对象数组转List
List list =Arrays.asList(users);
第二种方式
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
1. Java对象转字符串
String s =JSONObject.fromObject(obj).toString();
2 .Java对象列表转化为json对象数组
JSONArray array =JSONArray.fromObject(userList);
//转化为数组 array.toString();
3.字符串转java对象
str ="{\"lendperson\":\"李四\",\"lendcompany\":\"有限公司\",\"checkperson\":\"李四\",
\"lenddate\":\"2010-07-19T00:00:00\",\"lendcounts\":4,\"
passports\":[{\"passportid\":\"d\",\"name\":\"李豫川\",\"passporttype\":\"K\"},
{\"passportid\":\"K9051\",\"name\":\"李平\",\"passporttype\":\"K\"},
{\"passportid\":\"K90517\",\"name\":\"袁寒梅\",\"passporttype\":\"K\"},
{\"passportid\":\"K905199\",\"name\":\"贺明\",\"passporttype\":\"K\"}]}";
JSONObject jsonObject =JSONObject.fromObject(str);
User user =(User)JSONObject.toBean(jsonObject,User.class);
4.json字符串转为java对象数组
JSONArray json =JSONArray.fromObject(ss);
List<User> users =(List<User>)JSONArray.toCollection(json,User.class);