听闻阿里的fastJson特别的厉害.就下载了,稍微的试了一下:
本次使用:fastjson-1.2.8.jar
本次只是比较简单的对象和json之间的转换,没有涉及到复杂对象与json之间的转换.
有两个实体类,User和GroupBean,本次暂时只是涉及到User类
public class User {
private String id;
private String name;
private Integer age;
补充set和get方法}
public class GroupBean {
private String id;
private List list=new ArrayList();
private Set set=new HashSet();
private Map map=new HashMap();
补充set和get方法}
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
public class MyTest {
//简单对象的转换
@Test
public void test1() {
User user1 = new User("1","caocao",20);
User user2 = new User("2","liubei",22);
List list=new ArrayList();
Set set=new HashSet();
Map map=new HashMap();
list.add(user1);
list.add(user2);
set.add(user1);
set.add(user2);
map.put("user1", user1);
map.put("user2", user2);
//把对象转换成json
String jsonString1 = JSON.toJSONString(user1);
String jsonString2= JSON.toJSONString(user2);
System.out.println(jsonString1); //打印转换成json的字符串
//{"age":20,"id":"1","name":"caocao"}
System.out.println(user1); //直接打印对象
//User [id=1, name=caocao, age=20]
System.out.println(jsonString2);
//{"age":22,"id":"2","name":"liubei"}
System.out.println(user2);
//User [id=2, name=liubei, age=22]
String jsonString3 = JSON.toJSONString(list);
System.out.println(jsonString3);
//[{"age":20,"id":"1","name":"caocao"},{"age":22,"id":"2","name":"liubei"}]
String jsonString4 = JSON.toJSONString(set);
System.out.println(jsonString4);
//[{"age":20,"id":"1","name":"caocao"},{"age":22,"id":"2","name":"liubei"}]
String jsonString5 = JSON.toJSONString(map);
System.out.println(jsonString5);
//{"user2":{"age":22,"id":"2","name":"liubei"},"user1":{"age":20,"id":"1","name":"caocao"}}
//把json转换成bean
Object parse1 = JSON.parse(jsonString1); //直接进行解析
System.out.println(parse1);
//{"id":"1","name":"caocao","age":20}
//强转类型,不能进行强制类型的转换,会报出类转换异常的
//User parse2=(User)parse1;
User parseObject = JSON.parseObject(jsonString2, User.class); //添加类进行解析,直接得到bean
System.out.println(parseObject+"~~"+parseObject.getName());
//User [id=2, name=liubei, age=22]~~liubei
//直接解析出报错
// JSONObject parseObject2 = JSON.parseObject(jsonString3);
//添加类型解析
List parseObject2 = JSON.parseObject(jsonString3, new TypeReference>() {});
System.out.println(parseObject2);
//[User [id=1, name=caocao, age=20], User [id=2, name=liubei, age=22]]
//按照数据的类型解析
JSONArray parseArray = JSON.parseArray(jsonString3);
System.out.println(parseArray);
//[{"id":"1","name":"caocao","age":20},{"id":"2","name":"liubei","age":22}]
//解析出具体的map类型
Map parseObject5 = JSON.parseObject(jsonString5, new TypeReference>(){});
System.out.println(parseObject5);
//{user2=User [id=2, name=liubei, age=22], user1=User [id=1, name=caocao, age=20]}
//粗略的解析出了map
Map parseObject6 = JSON.parseObject(jsonString5, Map.class);
System.out.println(parseObject6);
//{user2={"id":"2","name":"liubei","age":22}, user1={"id":"1","name":"caocao","age":20}}
}
}
本次博客就先到这里,再过段时间我会把复杂和json之间的解析发出来.
如果错误,敬请指正
https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-18045781879.2.33c5236bZlPDUM&id=569353717228
深入理解Java虚拟机(jvm性能调优+内存模型+虚拟机原理) 很不错的虚拟机视频教程.