递归 json java_java处理json与对象的转化 递归

整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换!

对于对象json转换中遇到的问题我参考了一篇博客,http://blog.csdn.net/xq328220454/article/details/39256589

很有益处,我在文章的后半部分照搬了过来!

首先准备数据,准备了一堆具有主子关系的对象,包含普通属性id,父对象parent,子集合list,还有关联对象message;

1 @Before2 public void setUp() throwsException {3 Message message = new Message("name", 1);4 HeadCell hc1 = newHeadCell();5 HeadCell hc2 = newHeadCell();6 HeadCell hc11 = newHeadCell();7 HeadCell hc12 = newHeadCell();8 HeadCell hc21 = newHeadCell();9 HeadCell hc111 = newHeadCell();10 HeadCell hc112 = newHeadCell();11

12 hc111.setId("hc111");13 hc111.setMessage(message);14

15 hc112.setId("hc112");16 hc112.setMessage(message);17

18 hc11.setId("hc11");19 hc11.setMessage(message);20

21 hc12.setId("hc12");22 hc12.setMessage(message);23

24 hc21.setId("hc21");25 hc21.setMessage(message);26

27 hc1.setId("hc1");28 hc1.setMessage(message);29

30 hc2.setId("hc2");31 hc2.setMessage(message);32

33 List hcs11 = new ArrayList<>();34 hcs11.add(hc111);35 hcs11.add(hc112);36 hc11.setChildren(hcs11);37 hc111.setParent(hc11);38 hc112.setParent(hc11);39

40 List hcs1 = new ArrayList<>();41 hcs1.add(hc11);42 hcs1.add(hc12);43 hc1.setChildren(hcs1);44 hc11.setParent(hc1);45 hc12.setParent(hc1);46

47 List hcs2 = new ArrayList<>();48 hcs2.add(hc21);49 hc2.setChildren(hcs2);50

51 headCells.add(hc1);52 headCells.add(hc2);53 }

1 public classMessage {2 privateString name;3 privateInteger age;4

5 publicMessage() {}6

7 publicMessage(String name, Integer age) {8 this.name =name;9 this.age =age;10 }11

12 publicString getName() {13 returnname;14 }15

16 public voidsetName(String name) {17 this.name =name;18 }19

20 publicInteger getAge() {21 returnage;22 }23

24 public voidsetAge(Integer age) {25 this.age =age;26 }27

28 }

通过递归以目录格式解析层级关系,把准备的list以树形的方式打印处理

1   @Test2 public voidchart() {3 String str = "";4 buildChart(headCells, str);5 }6

7 private void buildChart(ListheadCells, String str) {8 str += "++";9 for (int i = 0; i < headCells.size(); i++) {10 HeadCell headCell =headCells.get(i);11 if (headCell.getChildren() != null && headCell.getChildren().size() > 0) {12 System.out.println(str +headCell.getId());13 buildChart(headCell.getChildren(), str);14 } else{15 System.out.println(str +headCell.getId());16 }17 }18 }

打印结果为:

++hc1++++hc11++++++hc111++++++hc112++++hc12++hc2++++hc21

处理List转化为jsonarray,当然对象是转换为jsonObject,还有一种是继承了ArrayList的对象,我有遇到过这种情况,忘了是怎么处理的了

这里转换的时候使用了config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);是因为parent自关联会无限递归下去,所以这解析出json的值

parent为空

1   @Test2 public voidproHeadCell2Json() {3 JsonConfig config = newJsonConfig();4 //config.setExcludes(new String[]{});

5 config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);6 try{7 String array =JSONArray.fromObject(headCells, config).toString();8 System.out.println(array);9 } catch(Exception e) {10 e.printStackTrace();11 }12 }

list对象转成json结果为

1 [ {2 "children": [ {3 "children": [ {4 "children": [],5 "id" : "hc111",6 "message": {7 "age" : 1,8 "name" : "name"

9 },10 "parent" : null

11 }, {12 "children": [],13 "id" : "hc112",14 "message": {15 "age" : 1,16 "name" : "name"

17 },18 "parent" : null

19 } ],20 "id" : "hc11",21 "message": {22 "age" : 1,23 "name" : "name"

24 },25 "parent" : null

26 }, {27 "children": [],28 "id" : "hc12",29 "message": {30 "age" : 1,31 "name" : "name"

32 },33 "parent" : null

34 } ],35 "id" : "hc1",36 "message": {37 "age" : 1,38 "name" : "name"

39 },40 "parent" : null

41 }, {42 "children": [ {43 "children": [],44 "id" : "hc21",45 "message": {46 "age" : 1,47 "name" : "name"

48 },49 "parent" : null

50 } ],51 "id" : "hc2",52 "message": {53 "age" : 1,54 "name" : "name"

55 },56 "parent" : null

57 } ]

把json转为list对象,这里主要是级联列并不能转为级联关系的对象,会直接报错,所以我们需要递归json把每一级都转为一个对象,并且在转换时把children添加

为例外,这里要知道我们的parent是空的,message并不存在级联列,所以可以直接存在对象里面。那么我们需要建立他们的父关系和子关系,还是要递归处理!

1   @Test2 public voidproJson2HeadCell() {3 JSONArray array =perpareHeadCell2Json();4 List headCells = new ArrayList<>();5 for (int i = 0; i < array.size(); i++) {6 JSONObject object =array.getJSONObject(i);7 headCells.add(buildTierObj(object));8 }9 }10

11 privateHeadCell buildTierObj(JSONObject object) {12 HeadCell hc = newHeadCell();13 if (!StringUtils.equals("[]", object.getString("children"))) {14 hc = (HeadCell) JSONObject.toBean(object, HeadCell.class);15 hc.setChildren(new ArrayList());16 hc.setParent(null);17 JSONArray array = object.getJSONArray("children");18 for (int i = 0; i < array.size(); i++) {19 HeadCell subHeadCell =buildTierObj(array.getJSONObject(i));20 subHeadCell.setParent(hc);21 hc.getChildren().add(subHeadCell);22 }23 } else{24 hc = (HeadCell) JSONObject.toBean(object, HeadCell.class);25 hc.setParent(null);26 }27 returnhc;28 }29

30 publicJSONArray perpareHeadCell2Json() {31 JsonConfig config = newJsonConfig();32 config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);33 try{34 JSONArray array =JSONArray.fromObject(headCells, config);35 returnarray;36 } catch(Exception e) {37 e.printStackTrace();38 }39 return null;40 }

下面是json转为对象中遇到的问题的总结:

在JSON-LIB中,要转换的对象包含自身对象时,会抛出异常There is a cycle in the hierarchy,解决办法,如下,这样不会保存自关联属性

1   JsonConfig config = newJsonConfig();2   config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

自定义要被转换的字段

1   JsonConfig config = newJsonConfig();2config.setJsonPropertyFilter(newPropertyFilter() {3@Override4 public booleanapply(Object arg0, String arg1, Object arg2) {5   if (arg1.equals("id") || arg1.equals("serialNumber") || arg1.equals("productName")) {6   return false;7 } else{8 return true;9 }10 }11 });

解决延迟加载产生异常的问题(net.sf.json.JSONException:java.lang.reflect.InvocationTargetException)

1 JsonConfig config = newJsonConfig();2 //解决延迟加载产生异常的问题

3 config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });

解决数据库查询结果中,Date转换的问题(net.sf.json.JSONException:java.lang.reflect.InvocationTargetException)

1 JsonConfig config = newJsonConfig();2 config.registerJsonValueProcessor(java.util.Date.class, newJsonValueProcessor() {3 @Override4 publicObject processArrayValue(Object obj, JsonConfig jsonconfig) {5 return null;6 }7

8 @Override9 publicObject processObjectValue(String key, Object value, JsonConfig jsonConfig) {10 if (value == null)11 return "";12 //注意:在判断几个父子级类型时要先判断子类型再判断父类型

13 if (value instanceofjava.sql.Date) {14 String str =DateFormat.getDateInstance(DateFormat.DEFAULT).format(value);15 returnstr;16 } else if (value instanceof java.sql.Timestamp || value instanceofjava.util.Date) {17 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");18 String str =format.format(value);19 returnstr;20 }21 returnvalue.toString();22 }23 });

有些字段的类型是枚举类型,可以在转换的时候将值设置为枚举类的value或者是label;

首先创建一个枚举类,这里我觉得我该写一篇关于枚举的博客,虽说不咋用。。。。。。

1 public enumGender {2 //通过括号赋值,而且必须带有一个参构造器和一个属性跟方法,否则编译出错3 //赋值必须都赋值或都不赋值,不能一部分赋值一部分不赋值;如果不赋值则不能写构造器,赋值编译也出错

4 MAN("MAN"), WOMEN("WOMEN");5

6 private finalString value;7

8 //构造器默认也只能是private, 从而保证构造函数只能在内部使用

9 Gender(String value) {10 this.value =value;11 }12

13 publicString getValue() {14 returnvalue;15 }16 }

配置config

1 JsonConfig config = newJsonConfig();2 config.registerJsonValueProcessor(Gender.class, newJsonValueProcessor() {3 @Override4 publicObject processObjectValue(String key, Object value, JsonConfig jsonConfig) {5 if (value instanceofGender) {6 Gender tmpValue =(Gender) value;7 returntmpValue.getValue();8 }9 returnvalue.toString();10 }11

12 @Override13 publicObject processArrayValue(Object arg0, JsonConfig arg1) {14 //TODO Auto-generated method stub

15 return null;16 }17 });

在处理json的时候可能有提供一些封装好的API,但是掌握基础是很重要的。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值