json的转换插件是通过java的一些工具,直接将java对象或集合转换成json字符串。
常用的json转换工具有如下几种:
1)jsonlib
2)Gson:google
3)fastjson:阿里巴巴
------GSON操作简单,只需要一个jar包----------
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPQAAAA3CAIAAAC0MfCzAAAGbklEQVR4nO2c708TZxzA+QP2bu/2Yq8WXgyNMS4TmyxbyH5pjC5LRiAbmGzm5guybIpuhYGOldISq6cUXNL5C4dMRFqdrQusI0jRiT+CP8qq15+09tpyhWopPDJM96L0+pTeXS3pKHf7fvKEXO9736dfkg8P32vuaVEcACRKUaELAID/CpAbkCwgNyBZQG5AsoDcgGQBuQHJIi65TUQRYYrH4xQpK5KR1OIRYSp0XcCqpAByz4a9o+3VltZtltZto+3Vs2HvC6eC3EAOFEDuodbtcwz1fG7y+dzkk4nb17XVL5yalDsFyA3wkn+5BwcHVSoVX3SWcQ03b/SP1E/fU0bvKSK39vv/2HtX98nNtq0327aOndzxbIbhnxvkBnIgz3KbzeaSkpLi4mK+C65p3g+M6SKOnqeu3qeu3qfO8xGqZ5o698RleOIyMONnRts/5p8eb0sSSieOTETRIiA6wJJPuRNmV1RUCMg90LgBBU0CY6BxA/87cMudUtpEgN4AS97kZs2urKwUkPtK/XrkO58YMVcXdeWb0Z8/Gj5SNnykbJgsGybL/lRtshzdbDm62XJ0S0Y238rNXgBdCpAiP3LjZgvLfenbdch9CrlPTd09PNKxOXjnCKIvIvoSoi+hwG8ocBkFjChomrJ1mtXvZGSD3EAO5EdulUpVjKFUKvmu7NuzFtnbw7eaBzVl4bHDyNOJPJ3IcwZN/IImutDEWeTtDt/T9re+x7juZGTztCVJndNeAP97VvqjwHNfr2Gu1w+o3mZGmxDVhqg2RGmRvR3ZO5D9GHL8xNxqMTWXTTpvc2XzrNwE3E8CHKy03F01rxv3b5wc2YusPyKrAlmb0bgSjbegcRX6Wx0aqbuseDfk4DQbAHJjpeW+rdecqSnpqV1/ft8bvd+9eUFe2icv7ZOXXpCX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BR9DxJqvNGfxEQqFGIZxuVwfEt9nTECRsrRP+sTntrgqXh2IQ+6BilczB51BKBTqMRjLa4XbElF5stjMwLK9HMQht8E8srNB89Znu7OOnQ0ag3mk0PUCqwJxyA0AywDkBiQLyA1IFpAbkCwgNyBZQG5AsoDcgGQBuQHJAnIDkgXkBiQLyA1IFpAbkCwgNyBZQG5AsvwLsh+etIhZt0wAAAAASUVORK5CYII=" alt="" />
User.java
package bean; public class User { private String id;
private String name;
private int age;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String id, String name, int age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
} }
1.测试Java对象转JSON(测试结果在后面注释)
操作简单,无论转单个对象还是集合,都是gson.toJson(obj);
package TestJson; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.xml.bind.UnmarshallerHandler; import org.junit.Test; import com.google.gson.Gson; import bean.User; public class Bean2JSON { // 测试单个对象转JSON
@Test
public void test1(){
User user = new User("1", "QIAOZHI",25,"山西太原");
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json); //{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}
} // 测试list转JSON
@Test
public void test2(){
List<User> list = new ArrayList<User>();
list.add(new User("1", "QIAOZHI",25,"山西太原"));
list.add(new User("2", "QIAOZHI",25,"山西太原"));
list.add(new User("3", "QIAOZHI",25,"山西太原"));
Gson gson = new Gson();
String json = gson.toJson(list);
System.out.println(json);
// [{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"}]
} // 测试map对象转JSON
@Test
public void test3(){
Map<String,User> map = new HashMap<String,User>();
map.put("1", new User("1", "QIAOZHI",25,"山西太原"));
map.put("2", new User("2", "QIAOZHI",25,"山西太原"));
map.put("3", new User("3", "QIAOZHI",25,"山西太原"));
Gson gson = new Gson();
String json = gson.toJson(map);
System.out.println(json);
// {"3":{"id":"3","name":"QIAOZHI","age":25,"address":"山西太原"},"2":{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},"1":{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}}
} // 测试list<map>对象转JSON
@Test
public void test4(){
List<Map<String,User>> list = new ArrayList<>();
Map<String,User> map1 = new HashMap<String,User>();
Map<String,User> map2 = new HashMap<String,User>();
map1.put("map11", new User("1", "QIAOZHI",25,"山西太原"));
map1.put("map12", new User("2", "QIAOZHI",25,"山西太原"));
map2.put("21", new User("11", "QIAOZHI",25,"山西太原"));
map2.put("22", new User("21", "QIAOZHI",25,"山西太原"));
list.add(map1);
list.add(map2);
Gson gson = new Gson();
String json = gson.toJson(list);
System.out.println(json);
// [{"map12":{"id":"2","name":"QIAOZHI","age":25,"address":"山西太原"},"map11":{"id":"1","name":"QIAOZHI","age":25,"address":"山西太原"}},{"21":{"id":"11","name":"QIAOZHI","age":25,"address":"山西太原"},"22":{"id":"21","name":"QIAOZHI","age":25,"address":"山西太原"}}]
} }
2.测试Json转Java对象与集合(测试结果在后面注释)
package TestJson_GSON; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Test; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class JSON2Bean {
// 测试json转单个对象
@Test
public void test1(){
String json = "{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user);
// User [id=1, name=QIAOZHI, age=25, address=山西太原]
} // 测试JSON转list
@SuppressWarnings("unchecked")
@Test
public void test2(){
String json = "[{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'},{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},{'id':'3','name':'QIAOZHI','age':25,'address':'山西太原'}]";
List<User> list = new ArrayList<User>();
Gson gson = new Gson();
list = (List<User>) gson.fromJson(json, new TypeToken<List<User>>(){}.getType());
System.out.println(list);
for(User user:list){
System.out.println(user);
}
// [{id=1, name=QIAOZHI, age=25.0, address=山西太原}, {id=2, name=QIAOZHI, age=25.0, address=山西太原}, {id=3, name=QIAOZHI, age=25.0, address=山西太原}]
System.out.println(list.get(1));
// {id=2, name=QIAOZHI, age=25.0, address=山西太原}
} // 测试JSON转Map
@Test
public void test3(){
String json = "{'3':{'id':'3','name':'QIAOZHI','age':25,'address':'山西太原'},'2':{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},'1':{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'}}";
Map<String,User> map = new HashMap<String,User>();
Gson gson = new Gson();
map = (Map<String,User>) gson.fromJson(json, new TypeToken<Map<String,User>>(){}.getType());
System.out.println(map);
// {3={id=3, name=QIAOZHI, age=25.0, address=山西太原}, 2={id=2, name=QIAOZHI, age=25.0, address=山西太原}, 1={id=1, name=QIAOZHI, age=25.0, address=山西太原}}
User user = map.get("1");
System.out.println(user);
// {id=1, name=QIAOZHI, age=25.0, address=山西太原}
} // 测试JSON转list<map>对象
@SuppressWarnings("unchecked")
@Test
public void test4(){
String json = "[{'map12':{'id':'2','name':'QIAOZHI','age':25,'address':'山西太原'},'map11':{'id':'1','name':'QIAOZHI','age':25,'address':'山西太原'}},{'21':{'id':'11','name':'QIAOZHI','age':25,'address':'山西太原'},'22':{'id':'21','name':'QIAOZHI','age':25,'address':'山西太原'}}]";
List<Map<String,User>> list = new ArrayList<>();
Gson gson = new Gson();
list = gson.fromJson(json, new TypeToken<List<Map<String,User>>>(){}.getType());
System.out.println(list);
// [{map12={id=2, name=QIAOZHI, age=25.0, address=山西太原}, map11={id=1, name=QIAOZHI, age=25.0, address=山西太原}}, {21={id=11, name=QIAOZHI, age=25.0, address=山西太原}, 22={id=21, name=QIAOZHI, age=25.0, address=山西太原}}]
System.out.println(list.get(1));
// {21={id=11, name=QIAOZHI, age=25.0, address=山西太原}, 22={id=21, name=QIAOZHI, age=25.0, address=山西太原}}
}
}
注意:
json转普通的Javabean只需要两个简单的参数,第一个是json字符串,第二个是需要转换对象的类型。
User user = gson.fromJson(json, User.class);
json转集合与map类型需要使用了TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换。也就是通知GSON将json转为那种集合
list = (List<User>) gson.fromJson(json, new TypeToken<List<User>>(){}.getType());
map = (Map<String,User>) gson.fromJson(json, new TypeToken<Map<String,User>>(){}.getType());
list = gson.fromJson(json, new TypeToken<List<Map<String,User>>>(){}.getType());