目录
1、json-lib:是 apache提供的json-lib小工具,它可以方便的使用Java语言来创建JSON字符串。也可以把JavaBean转换成JSON字符串。
2、jackson:是用来解析 JSON 的框架。JSON (JavaScript Object Notation)是一种数据格式,用来在客户端和服务器之间,或系统组件之间交换数据。
3、Gson:google提供的一个json类型转换类,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
4、fastJson:阿里巴巴推出的一种轻量级的数据交换格式。它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。它采用一种 “键 : 值” 对的文本格式来存储和表示数据,在系统交换数据过程中常常被使用,是一种理想的数据交换语言。
@Test
public void bean2string() {
try {
Emp emp = new Emp("admin", "123456", "李昊哲", 1, BaseDate.parse("1983-11-22"));
String jsonString = JSON.toJSONString(emp);
System.out.println(jsonString);
} catch (ParseException e) {
e.printStackTrace();
}
}
@Test
public void string2bean() {
String text = "{\"account\":\admin\",\"birthday\":\"1983-11-22\",\"gender\":1,\"nickname\":\"李昊哲\".\"password\":\"123456\"}"
Emp emp = JSON.parseObjedct(text, Emp.class);
System.out.println(emp.toString());
}
@Test
public void list2string(){
try{
List<Emp> list= new ArrayList<Emp>();
list.add(new Emp("admin","123456","李昊哲",1,BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix","123456","phoenix",1,BasseDate.parse("1983-11-22 20:30:00")));
String jsonString =g.toJson(list);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
@Test
public void string2list(){
try{
List<Emp> list= new ArrayList<Emp>();
list.add(new Emp("admin","123456","李昊哲",1,BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix","123456","phoenix",1,BasseDate.parse("1983-11-22 20:30:00")));
String jsonString =g.toJson(list);
System.out.println(jsonString);
List<Emp> empList = g.son.fromJson(jsonString,new Type TOken<Emp>>)(){}.getTYpe());
empList.forEach(emp->{
System.out.println(emp);
});
}catch (ParseException e){
e.printStackTrace();
}
}
@Test
public void map2string(){
try {
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp("admin", "123456", "李昊哲", 1, BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix", "123456", "phoenix", 1, BasseDate.parse("1983-11-22 20:30:00")));
Map<String, List<Emp>> map = new HashMap<~> ();
map.put("emps", list);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
@Test
public void string2map(){
try{
List<Emp> list= new ArrayList<Emp>();
list.add(new Emp("admin", "123456", "李昊哲", 1, BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix", "123456", "phoenix", 1, BasseDate.parse("1983-11-22 20:30:00")));
Map<String ,List<Emp>> map = new HashMap<~>();
map.put("emps",list);
String jsonString = gson.toJson(map);
System.out.println(jsonString);
Map<String ,list<Emp>> stringListHashMap=gson.fromJson(jsonString,new TypeToken<Map<String,List<Emp>>>){}.getType());
if(StringLishtHashMap.containskey("emps")){
List<Emp> emps=stringListHashMap.get("emps");
emps.forEach(emp->){
System.out.println(emp.toSTRING());
});
}
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
@Test
public void bean2string(){
try{
Date birthday = BaseDate.parse("1983-11-22 20:30:00");
Emp emp = new Emp("admin","123456","李昊哲",1,BaseDate.parse("1983-11-22 20:30:00"));
String jsonString =gson.toJson(emp);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
@Test
public void list2string(){
try{
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp("admin","123456","李昊哲",1,BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix","123456","phoenix",1,BasseDate.parse("1983-11-22 20:30:00")));
String jsonString =gson.toJson(list);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}
@Test
public void string2list(){
try{
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp("admin","123456","李昊哲",1,BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix","123456","phoenix",1,BasseDate.parse("1983-11-22 20:30:00")));
String jsonString =gson.toJson(list);
List<Emp> empList = gson.fromJson(jsonString,new TypeToken<List<Emp>>(){}.getType());
empList.forEach(emp ->{
System.out.println(emp);
});
}catch (ParseException e){
}
}
4.从map类型到string类型
@Test
public void map2string(){
try{
List<Emp> list = new ArrayList<Emp>();
list.add(new Emp("admin","123456","李昊哲",1,BaseDate.parse("1983-11-22 20:30:00")));
List.add(new Emp("phoenix","123456","phoenix",1,BasseDate.parse("1983-11-22 20:30:00")));
Map<String>,List<Emp> map = new HashMap<~>();
map.put("emp",list);
String jsonString = gson.toJson(map);
System.out.println(jsonString);
}catch (ParseException e){
e.printStackTrace();
}
}