fastJson 是阿里巴巴 开源的json处理工具。使用面向对象与注解的方式,很容易使用。
两个基本的方法:
对象序列化为json串 String jsonStr = JSON.toJSONString(obj);
json串反序列化为对象:Object obj = JSON.parseObject(jsonStr,Object.class); //Object 可以为任意对象
实体类映射实例:
1.使用注解配置实体对象
ordinal 为顺序,越小则排列越前,不配置的话则按默认排序,与字段排序不一致。
其中进行了组合,包括组合list
@JSONField(ordinal=9)
private MainVo main; //引入另外一个实体对象,其中同样配置
@JSONField(ordinal=10)
private String AppliName;
@JSONField(ordinal=20)
private String AppliIdNo;
@JSONField(ordinal=30,format = "yyyy-MM-dd hh:mm:ss")
private Date startDate;
@JSONField(ordinal=40)
private double Premium;
@JSONField(ordinal=50)
private List<ItemKindVo> itemKindList; //引入实体对象集合,fastJSON将其处理为数组
2. 序列化和反序列化
//序列化为json字符
@Test
public void json4() {
PolicyVo policyVo = new PolicyVo("cai","1231",new Date(),100.89);
ItemKindVo itemKindVo = new ItemKindVo("水","er",1,100,new Date());
ItemKindVo itemKindVo2 = new ItemKindVo("菜","er",1,100,new Date());
List<ItemKindVo> list = new ArrayList<ItemKindVo>();
list.add(itemKindVo);
list.add(itemKindVo2);
policyVo.setItemKindList(list);
MainVo main = new MainVo("20000000000001","主");
policyVo.setMain(main);
String jsonStr = JSON.toJSONString(policyVo); //根据对象进行序列化
System.out.println(jsonStr);
}
//输出结果为
{"main":{"main1":"主","policyno":"20000000000001"},"appliName":"cai","appliIdNo":"1231","startDate":"2020-05-06 02:34:07","premium":100.89,"itemKindList":[{"kindName":"水","itemName":"er","quantity":1.0,"premium":100.0,"date":"2020-05-06 02:34:07"},{"kindName":"菜","itemName":"er","quantity":1.0,"premium":100.0,"date":"2020-05-06 02:34:07"}]}
//Json字符串反序列化为对象
@Test
public void json5() {
String jsonStr ="{\"appliIdNo\":\"1231\",\"startDate\":\"2020-05-06 11:20:43\",\"premium\":100.89,\"itemKindList\":[{\"kindName\":\"水稻\",\"itemName\":\"er\",\"quantity\":1.0,\"premium\":100.0,\"date\":\"2020-05-06 11:20:43\"},{\"kindName\":\"蔬菜\",\"itemName\":\"er\",\"quantity\":1.0,\"premium\":100.0,\"date\":\"2020-05-06 11:20:43\"}]}\r\n" +
"";
PolicyVo policyVo = JSON.parseObject(jsonStr,PolicyVo.class); //反序列化操作
System.out.println(policyVo);
}
对象操作:
JSONObject 与 JSONArray
JSONObject 将 json串转换为对象进行操作
JSONArray 将 json串转换为多个JSONObject对象 进行操作
JSONObject jsonObject = JSON.parseObject(jsonStr); // 字符转对象
String appliName = jsonObject.getString("appliName"); //根据key获取内容,内容也可是部分json串
JSONArray array = JSON.parseArray(itemKindList); //字符转对象数组
JSONObject j2 = array.getJSONObject(0); // 通过下标获取对象
实例:
JSONObject jsonObject = JSON.parseObject(jsonStr); // 将json字符串 转换为JSONObject
String appliName = jsonObject.getString("appliName"); //根据Key获取对应内容
System.out.println(appliName);
String jsonmain = jsonObject.getString("main"); //获取其中的部分json
JSONObject jsonObjectmain = JSON.parseObject(jsonmain); // 将json字符串 转换为JSONObject
String main1 = jsonObjectmain.getString("main1"); //根据Key获取对应内容
System.out.println(main1);
String itemKindList = jsonObject.getString("itemKindList"); //获取其中的部分json 这部分是个数组
JSONArray array = JSON.parseArray(itemKindList);
JSONObject j2 = array.getJSONObject(0); // 通过下标获取对象
String kindName = j2.getString("kindName"); //根据Key获取对应内容
System.out.println(kindName);