一 使用json-lib包下的JSONObject和JSONArray转换
代码如下:
package com.test.test;
import java.util.ArrayList;
import java.util.List;
import com.cn.entity.User;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* JSONObject和JSONArray
* @author sun.kai
* 2016年8月7日
*/
public class TestJsonOne {
public static void main(String[] args){
long time1 = System.currentTimeMillis();
// Map<String, Object> param = new HashMap<String,Object>();
// param.put("id", 1);
// param.put("userName", "admin管理员");
// param.put("password", "admin123456");
// param.put("age", 25);
// JSONObject jsonObject = JSONObject.fromObject(param);
// System.out.println(jsonObject);
// JSONArray jsonArray = JSONArray.fromObject(param);
// System.out.println(jsonArray);
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setUserName("admin管理员");
user1.setPassword("admin123456");
user1.setAge(25);
testJsonObject(user1);
list.add(user1);
User user2 = new User();
user2.setId(2);
user2.setUserName("海角七号");
user2.setPassword("海角七号123456");
user2.setAge(26);
list.add(user2);
testJsonArray(list);
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
@SuppressWarnings("static-access")
public static void testJsonObject(User user){
System.out.println("JSONObject测试");
/*
* java对象转换成json对象
*/
JSONObject jsonObject1 = JSONObject.fromObject(user);
/*
* json对象转成字符串
*/
String jsonsString1 = jsonObject1.toString();
System.out.println(jsonObject1);
System.out.println(jsonsString1);
String jsonString2 = "{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"}";
/*
* 字符串转成json对象
*/
JSONObject jsonObject2 = JSONObject.fromObject(jsonString2);
/*
* json对象转成java对象
*/
User user2 = (User) jsonObject2.toBean(jsonObject2,User.class);
System.out.println(user2.getUserName());
/*
* 获取json对象中的json对象数组
*/
String jsonString3 = "{\"abc\":[{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"},"
+ "{\"age\":26,\"id\":2,\"password\":\"海角七号123456\",\"userName\":\"海角七号\"}"
+ "]}";
//字符串转成json对象
JSONObject jsonObject3 = JSONObject.fromObject(jsonString3);
//获取json对象中的json对象数组
JSONArray jsonArray = jsonObject3.getJSONArray("abc");
System.out.println(jsonArray.toString());
}
@SuppressWarnings("unchecked")
public static void testJsonArray(List<User> userList){
System.out.println("JSONArray测试");
/*
* java对象列表转换为json对象数组
*/
JSONArray jsonArray1 = JSONArray.fromObject(userList);
/*
* json对象数组转成字符串
*/
String arrayString1 = jsonArray1.toString();
System.out.println(jsonArray1);
System.out.println(arrayString1);
String arrayString2 = "[{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"},{\"age\":26,\"id\":2,\"password\":\"海角七号123456\",\"userName\":\"海角七号\"}]";
/*
* 字符串转成json对象数组
*/
JSONArray jsonArray2 = JSONArray.fromObject(arrayString2);
/*
* json对象数组转成java对象
*/
List<User> users = (List<User>) JSONArray.toCollection(jsonArray2, User.class);
for(User user : users){
System.out.println(user.getUserName());
}
/*
* 循环json对象数组,获取json对象
*/
for(int i = 0;i<jsonArray2.size();i++){
JSONObject jsonObject = jsonArray2.getJSONObject(i);
System.out.println(jsonObject);
}
}
}
User实体类代码:
package com.cn.entity;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = -154635138564326678L;
private Integer id;
private String userName;
private String password;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
执行后的结果:
JSONObject测试
{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"}
{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"}
admin管理员
[{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"},{"age":26,"id":2,"password":"海角七号123456","userName":"海角七号"}]
JSONArray测试
[{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"},{"age":26,"id":2,"password":"海角七号123456","userName":"海角七号"}]
[{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"},{"age":26,"id":2,"password":"海角七号123456","userName":"海角七号"}]
admin管理员
海角七号
{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"}
{"age":26,"id":2,"password":"海角七号123456","userName":"海角七号"}
686
二 使用jackson-databind包下的ObjectMapper
代码如下:
package com.test.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.cn.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* jackson是spring mvc内置的json转换工具
* @author sun.kai
* 2016年8月7日
*/
public class TestJsonTwo {
public static void main(String[] args) throws JsonProcessingException{
long time1 = System.currentTimeMillis();
List<User> users = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setUserName("admin管理员");
user1.setPassword("admin123456");
user1.setAge(25);
users.add(user1);
User user2 = new User();
user2.setId(2);
user2.setUserName("海角七号");
user2.setPassword("海角七号123456");
user2.setAge(26);
users.add(user2);
try {
testObjectMapper(users);
} catch (IOException e) {
e.printStackTrace();
}
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
public static void testObjectMapper(List<User> users) throws IOException{
ObjectMapper mapper = new ObjectMapper();
/*
* java对象是一个集合
* 转成的字符串是json对象数组
*/
String mapperString1 = mapper.writeValueAsString(users);
System.out.println(mapperString1);
/*
* java对象不是一个集合
* 转成json对象字符串
*/
String mapperString2 = mapper.writeValueAsString(users.get(0));
System.out.println(mapperString2);
/*
* json对象数组字符串转java对象集合
*/
String jsonString1 = "[{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"},{\"age\":26,\"id\":2,\"password\":\"海角七号123456\",\"userName\":\"海角七号\"}]";
User[] userArray = mapper.readValue(jsonString1, User[].class);
List<User> userList = Arrays.asList(userArray);
for (User user : userList) {
System.out.println(user.getUserName());
}
/*
* json对象字符串转java对象
*/
String jsonString2 = "{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"}";
User user = mapper.readValue(jsonString2, User.class);
System.out.println(user.getUserName());
}
}
执行后的结果:
[{"id":1,"userName":"admin管理员","password":"admin123456","age":25},{"id":2,"userName":"海角七号","password":"海角七号123456","age":26}]
{"id":1,"userName":"admin管理员","password":"admin123456","age":25}
admin管理员
海角七号
admin管理员
784
三 使用fastjson
代码如下:
package com.test.test;
import com.cn.entity.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
/**
* fastjson是阿里做的国有开源Java工具包
* @author sun.kai
* 2016年8月7日
*/
public class TestJsonThree {
public static void main(String[] args){
long time1 = System.currentTimeMillis();
List<User> users = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setUserName("admin管理员");
user1.setPassword("admin123456");
user1.setAge(25);
users.add(user1);
User user2 = new User();
user2.setId(2);
user2.setUserName("海角七号");
user2.setPassword("海角七号123456");
user2.setAge(26);
users.add(user2);
testFastjson(users);
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
@SuppressWarnings("unchecked")
public static void testFastjson(List<User> users){
/*
* java对象是一个集合
* 转成的字符串是json对象数组
*/
String fastjsonString1 = JSON.toJSONString(users);
System.out.println(fastjsonString1);
/*
* java对象不是一个集合
* 转成json对象字符串
*/
String fastjsonString2 = JSON.toJSONString(users.get(0));
System.out.println(fastjsonString2);
/*
* json对象数组字符串转java对象集合
*/
String jsonString1 = "[{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"},{\"age\":26,\"id\":2,\"password\":\"海角七号123456\",\"userName\":\"海角七号\"}]";
List<User> userList = JSON.parseArray(jsonString1, User.class);
for (User user : userList) {
System.out.println(user.getUserName());
}
/*
* json对象数组字符串转Map集合
*/
List<Map<String, Object>> maps = JSON.parseObject(jsonString1,new TypeReference<List<Map<String,Object>>>(){});
System.out.println(maps);
for (Map<String, Object> map : maps) {
System.out.println(map.get("userName"));
}
/*
* json对象字符串转java对象
*/
String jsonString2 = "{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"}";
User user = JSON.parseObject(jsonString2, User.class);
System.out.println(user.getUserName());
/*
* json对象字符串转Map
*/
Map<String, Object> map = JSON.parseObject(jsonString2, Map.class);
System.out.println(map);
System.out.println(map.get("userName"));
}
}
执行结果:
[{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"},{"age":26,"id":2,"password":"海角七号123456","userName":"海角七号"}]
{"age":25,"id":1,"password":"admin123456","userName":"admin管理员"}
admin管理员
海角七号
[{id=1, age=25, userName=admin管理员, password=admin123456}, {id=2, age=26, userName=海角七号, password=海角七号123456}]
admin管理员
海角七号
admin管理员
{id=1, age=25, userName=admin管理员, password=admin123456}
admin管理员
338
四 使用Google的Gson
代码如下:
package com.test.test;
import com.cn.entity.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* google的gson
* https://mvnrepository.com/artifact/com.google.code.gson/gson
* @author sun.kai
* 2016年8月7日
*/
public class TestJsonFour {
public static void main(String[] args){
long time1 = System.currentTimeMillis();
List<User> users = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setUserName("admin管理员");
user1.setPassword("admin123456");
user1.setAge(25);
users.add(user1);
User user2 = new User();
user2.setId(2);
user2.setUserName("海角七号");
user2.setPassword("海角七号123456");
user2.setAge(26);
users.add(user2);
testGson(users);
long time2 = System.currentTimeMillis();
System.out.println(time2-time1);
}
@SuppressWarnings("unchecked")
public static void testGson(List<User> users){
/*
* 创建Gson对象
*/
Gson gson = new Gson();
/*
* 获得泛型参数的类型
*/
java.lang.reflect.Type type = new TypeToken<List<User>>() {}.getType();
/*
* java对象是一个集合
* 转成的字符串是json对象数组
*/
String gsonString1 = gson.toJson(users);
System.out.println(gsonString1);
/*
* java对象不是一个集合
* 转成json对象字符串
*/
String gsonString2 = gson.toJson(users.get(0));
System.out.println(gsonString2);
/*
* json对象数组字符串转java对象集合
*/
String jsonString1 = "[{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"},{\"age\":26,\"id\":2,\"password\":\"海角七号123456\",\"userName\":\"海角七号\"}]";
List<User> userList = gson.fromJson(jsonString1, type);
for (User user : userList) {
System.out.println(user.getUserName());
}
/*
* json对象数组字符串转Map集合
*/
List<Map<String, Object>> maps = gson.fromJson(jsonString1,new TypeToken<List<Map<String, Object>>>() {}.getType());
System.out.println(maps);
for (Map<String, Object> map : maps) {
System.out.println(map.get("userName"));
}
/*
* json对象字符串转java对象
*/
String jsonString2 = "{\"age\":25,\"id\":1,\"password\":\"admin123456\",\"userName\":\"admin管理员\"}";
User user = gson.fromJson(jsonString2, User.class);
System.out.println(user.getUserName());
/*
* json对象字符串转Map
*/
Map<String, Object> map = gson.fromJson(jsonString2, Map.class);
System.out.println(map);
System.out.println(map.get("userName"));
}
}
执行结果:
[{"id":1,"userName":"admin管理员","password":"admin123456","age":25},{"id":2,"userName":"海角七号","password":"海角七号123456","age":26}]
{"id":1,"userName":"admin管理员","password":"admin123456","age":25}
admin管理员
海角七号
[{age=25.0, id=1.0, password=admin123456, userName=admin管理员}, {age=26.0, id=2.0, password=海角七号123456, userName=海角七号}]
admin管理员
海角七号
admin管理员
{age=25.0, id=1.0, password=admin123456, userName=admin管理员}
admin管理员
336