fastjson学习

Posted by New Boy on June 9, 2018

前言

额,json的介绍就不多说了,非常方便。对于json相关的处理也是工作中非常基础的一部分。不过之前非常乱,最近搜了搜资料,对fastjson做了一个总结,相信以后的json格式上应该是没什么问题了。

主要内容

概括

先概括一下主要的知识点,理一理思绪,对下一步的学习还是十分关键的。

1.测试中我们会接触到三种对象,分别的JSON对象,json串,javabean。JSON对象就是JSONObject,fastjson框架中提供的json对象,剩下2个就不说了。

2.测试的对象主要包括:简单json,复杂json,json数组

显而易见,我们测试的内容就是分别针对(简单json,复杂json,json数组)做(JSON对象,json串,javabean)的两两转换。

简单json

先放上简单json的测试数据:

//javabean,省略构造方法,set,get等
public class Simple implements Serializable {
    private String name;
    private String sex;
    private Integer age;
}
static {
    simpleBean = new Simple();
    simpleBean.setAge(15);
    simpleBean.setName("fff");
    simpleBean.setSex("male");
}
//json串
public static String simpleJson = "{\"name\": \"ttt\",\"age\": 12,\"sex\": \"male\"}";

JSON <==> json串

//JSON=>json
JSONObject jsonObject = JSONObject.parseObject(simpleJson);
String json = jsonObject.toJSONString();
System.out.println(json);//{"sex":"male","name":"ttt","age":12}
//JSON<=json
JSONObject jsonObject = JSONObject.parseObject(simpleJson);
System.out.println(jsonObject.get("sex"));//male

JSON <==> javabean

//JSON=>javabean
JSONObject jsonObject2 = JSONObject.parseObject(complexJson);
Simple simple1 = JSONObject.parseObject(jsonObject1.toJSONString(), new TypeReference<Simple>() {});
System.out.println(simple1);//Simple{name='ttt', sex='male', age=12}
//JSON<=javabean
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(simpleBean));
System.out.println(jsonObject.get("name"));

Json串 <==> javabean

//json串=>javabean
Simple simple = JSONObject.parseObject(simpleJson, Simple.class);
System.out.println(simple);//Simple{name='ttt', sex='male', age=12}
Simple simple1 = JSONObject.parseObject(simpleJson, new TypeReference<Simple>() {});
System.out.println(simple1);//Simple{name='ttt', sex='male', age=12}
//json串<=javabean
String jsonString = JSON.toJSONString(simpleBean);
System.out.println(jsonString);//{"age":15,"name":"fff","sex":"male"}

复杂json

测试数据:

//javabean
public class Complex {
    private Integer index;
    private List<Simple> simples;
    private List<Integer> grades;
}
static {
    complexBean = new Complex();
    complexBean.setGrades(Arrays.asList(1, 2, 4, 5, 6));
    complexBean.setIndex(1);
    complexBean.setSimples(Arrays.asList(new Simple("ggg", "male", 17)
                                         , new Simple("ttt", "male", 18)
                                         , new Simple("yyy", "male", 19)
                                        ));
}
//json串
public static String complexJson = "{\"simples\":[{\"name\": \"ttt\",\"age\": 12,\"sex\": \"male\"},{\"name\": \"fff\",\"age\": 13,\"sex\": \"female\"}],\"index\":5,\"grades\":[12,32,43,51]}";

JSON <==> json串

//JSON=>json
JSONObject jsonObject = JSONObject.parseObject(complexJson);
String json = jsonObject.toJSONString();
System.out.println(json);
//JSON<=json
JSONObject jsonObject = JSONObject.parseObject(complexJson);
System.out.println(jsonObject.get("grades"));//[12,32,43,51]
System.out.println(jsonObject.toString());//{"index":5,"grades":[12,32,43,51],"simples":[{"sex":"male","name":"ttt","age":12},{"sex":"female","name":"fff","age":13}]}

if (jsonObject.get("simples") != null) {
  System.out.println(JSONObject.parseObject(JSONArray.parseArray(jsonObject.get("simples").toString()).get(1).toString()).get("sex"));//female
}

JSON <==> javabean

//JSON=>javabean
JSONObject jsonObject2 = JSONObject.parseObject(complexJson);
Complex complex1 = JSONObject.parseObject(jsonObject2.toJSONString(), new TypeReference<Complex>() {});
System.out.println(complex1);//Complex{index=5, simples=[Simple{name='ttt', sex='male', age=12}, Simple{name='fff', sex='female', age=13}], grades=[12, 32, 43, 51]}
//JSON<=javabean
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(complexBean));
System.out.println(jsonObject.get("grades"));//[1,2,4,5,6]
System.out.println(jsonObject.get("simples"));//[{"sex":"male","name":"ggg","age":17},{"sex":"male","name":"ttt","age":18},{"sex":"male","name":"yyy","age":19}]

Json串 <==> javabean

//json串=>javabean,有两种方式
Complex complex = JSONObject.parseObject(complexJson, Complex.class);
System.out.println(complex);//Complex{index=5, simples=[Simple{name='ttt', sex='male', age=12}, Simple{name='fff', sex='female', age=13}], grades=[12, 32, 43, 51]}
Complex complex1 = JSONObject.parseObject(complexJson, new TypeReference<Complex>() {
});
System.out.println(complex1);
//json串<=javabean
String jsonString1 = JSONObject.toJSONString(complexBean);
System.out.println(jsonString1);//{"grades":[1,2,4,5,6],"index":1,"simples":[{"age":17,"name":"ggg","sex":"male"},{"age":18,"name":"ttt","sex":"male"},{"age":19,"name":"yyy","sex":"male"}]}

json数组

测试数据:

//javabean
static {
    Simple simple0 = new Simple();
    simple0.setAge(15);
    simple0.setName("fff");
    simple0.setSex("male");
    Simple simple1 = new Simple();
    simple1.setSex("female");
    simple1.setAge(123);
    simple1.setName("asdas");
    arrayBean.add(simple0);
    arrayBean.add(simple1);
}
//json串
public static String arrayJson = "[{\"name\": \"ttt\",\"age\": 12,\"sex\": \"male\"},{\"name\": \"fff\",\"age\": 13,\"sex\": \"female\"}]";

JSON <==> json串

//json==>JSON
JSONArray array = JSONArray.parseArray(arrayJson);
JSONObject object = array.getJSONObject(1);
System.out.println(object.get("sex"));//female
//json<==JSON
String json = JSONArray.toJSONString(array);
System.out.println(json);//[{"sex":"male","name":"ttt","age":12},{"sex":"female","name":"fff","age":13}]

JSON <==> javabean

//JSON==>javabean
JSONArray array = JSONArray.parseArray(arrayJson);
List<Simple> simples = JSONArray.parseArray(array.toJSONString(), Simple.class);
System.out.println(simples);//[Simple{name='ttt', sex='male', age=12}, Simple{name='fff', sex='female', age=13}]
//JSON<==javabean
JSONArray array1 = JSONArray.parseArray(JSONArray.toJSONString(arrayBean));
JSONObject object = array.getJSONObject(1);
System.out.println(object.get("sex"));//female

Json串 <==> javabean

//json=>javabean
//第一种方式
List<Simple> simples = JSONArray.parseArray(arrayJson, Simple.class);
System.out.println(simples);//[Simple{name='ttt', sex='male', age=12}, Simple{name='fff', sex='female', age=13}]
//第二种方式
JSONArray array = JSONArray.parseArray(arrayJson);
Type[] types = new Type[array.size()];
Arrays.fill(types, Map.class);
List<Object> simples1 = JSONArray.parseArray(arrayJson, types);
simples1.forEach(simples2 -> {
    Map<String, Object> asd = (Map<String, Object>) simples2;
    System.out.println(asd.get("name"));
});//ttt fff
//json<=javabean
String str = JSON.toJSONString(arrayBean);
System.out.println(str);//[{"age":15,"name":"fff","sex":"male"},{"age":123,"name":"asdas","sex":"female"}]

总结

整体测试下来,对于简单json和复杂json来说,其实两者没什么差别,其中转化过程中主要就是涉及到了JSONObject类,涉及到的方法就是JSONObject.parseObject()方法,它可以将string格式转为JSONObject或着bean对象。而对于JSONArray来说,也是大同小异,只不过是用JSONArray.parseArray()。另外还有一个方法,就是将bean转为String的函数:JSONObject.toJSONString(),JSONArray.toJSONString,JSON.toJSONString。看源码可以发现,前两者实际上都是在调用JSON.toJSONString()。总的来说,主要是这些东西,还是要在实际工作中不断应用总结。