java处理json与对象的转化 递归
整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换!
对于对象json转换中遇到的问题我参考了一篇博客,http://blog.csdn.net/xq328220454/article/details/39256589
很有益处,我在文章的后半部分照搬了过来!
首先准备数据,准备了一堆具有主子关系的对象,包含普通属性id,父对象parent,子集合list<HeadCell>,还有关联对象message;
@Before
public void setUp() throws Exception {
Message message = new Message("name", 1);
HeadCell hc1 = new HeadCell();
HeadCell hc2 = new HeadCell();
HeadCell hc11 = new HeadCell();
HeadCell hc12 = new HeadCell();
HeadCell hc21 = new HeadCell();
HeadCell hc111 = new HeadCell();
HeadCell hc112 = new HeadCell(); hc111.setId("hc111");
hc111.setMessage(message); hc112.setId("hc112");
hc112.setMessage(message); hc11.setId("hc11");
hc11.setMessage(message); hc12.setId("hc12");
hc12.setMessage(message); hc21.setId("hc21");
hc21.setMessage(message); hc1.setId("hc1");
hc1.setMessage(message); hc2.setId("hc2");
hc2.setMessage(message); List<HeadCell> hcs11 = new ArrayList<>();
hcs11.add(hc111);
hcs11.add(hc112);
hc11.setChildren(hcs11);
hc111.setParent(hc11);
hc112.setParent(hc11); List<HeadCell> hcs1 = new ArrayList<>();
hcs1.add(hc11);
hcs1.add(hc12);
hc1.setChildren(hcs1);
hc11.setParent(hc1);
hc12.setParent(hc1); List<HeadCell> hcs2 = new ArrayList<>();
hcs2.add(hc21);
hc2.setChildren(hcs2); headCells.add(hc1);
headCells.add(hc2);
}
public class Message {
private String name;
private Integer age;
public Message() {}
public Message(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
通过递归以目录格式解析层级关系,把准备的list以树形的方式打印处理
@Test
public void chart() {
String str = "";
buildChart(headCells, str);
} private void buildChart(List<HeadCell> headCells, String str) {
str += "++";
for (int i = 0; i < headCells.size(); i++) {
HeadCell headCell = headCells.get(i);
if (headCell.getChildren() != null && headCell.getChildren().size() > 0) {
System.out.println(str + headCell.getId());
buildChart(headCell.getChildren(), str);
} else {
System.out.println(str + headCell.getId());
}
}
}
打印结果为:
++hc1
++++hc11
++++++hc111
++++++hc112
++++hc12
++hc2
++++hc21
处理List<HeadCell>转化为jsonarray,当然对象是转换为jsonObject,还有一种是继承了ArrayList的对象,我有遇到过这种情况,忘了是怎么处理的了
这里转换的时候使用了config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);是因为parent自关联会无限递归下去,所以这解析出json的值
parent为空
@Test
public void proHeadCell2Json() {
JsonConfig config = new JsonConfig();
// config.setExcludes(new String[]{});
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
try {
String array = JSONArray.fromObject(headCells, config).toString();
System.out.println(array);
} catch (Exception e) {
e.printStackTrace();
}
}
list对象转成json结果为
[ {
"children" : [ {
"children" : [ {
"children" : [],
"id" : "hc111",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
}, {
"children" : [],
"id" : "hc112",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
} ],
"id" : "hc11",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
}, {
"children" : [],
"id" : "hc12",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
} ],
"id" : "hc1",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
}, {
"children" : [ {
"children" : [],
"id" : "hc21",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
} ],
"id" : "hc2",
"message" : {
"age" : 1,
"name" : "name"
},
"parent" : null
} ]
把json转为list对象,这里主要是级联列并不能转为级联关系的对象,会直接报错,所以我们需要递归json把每一级都转为一个对象,并且在转换时把children添加
为例外,这里要知道我们的parent是空的,message并不存在级联列,所以可以直接存在对象里面。那么我们需要建立他们的父关系和子关系,还是要递归处理!
@Test
public void proJson2HeadCell() {
JSONArray array = perpareHeadCell2Json();
List<HeadCell> headCells = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JSONObject object = array.getJSONObject(i);
headCells.add(buildTierObj(object));
}
} private HeadCell buildTierObj(JSONObject object) {
HeadCell hc = new HeadCell();
if (!StringUtils.equals("[]", object.getString("children"))) {
hc = (HeadCell) JSONObject.toBean(object, HeadCell.class);
hc.setChildren(new ArrayList<HeadCell>());
hc.setParent(null);
JSONArray array = object.getJSONArray("children");
for (int i = 0; i < array.size(); i++) {
HeadCell subHeadCell = buildTierObj(array.getJSONObject(i));
subHeadCell.setParent(hc);
hc.getChildren().add(subHeadCell);
}
} else {
hc = (HeadCell) JSONObject.toBean(object, HeadCell.class);
hc.setParent(null);
}
return hc;
} public JSONArray perpareHeadCell2Json() {
JsonConfig config = new JsonConfig();
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
try {
JSONArray array = JSONArray.fromObject(headCells, config);
return array;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
下面是json转为对象中遇到的问题的总结:
在JSON-LIB中,要转换的对象包含自身对象时,会抛出异常There is a cycle in the hierarchy,解决办法,如下,这样不会保存自关联属性
JsonConfig config = new JsonConfig();
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
自定义要被转换的字段
JsonConfig config = new JsonConfig();
2 config.setJsonPropertyFilter(new PropertyFilter() {
@Override
public boolean apply(Object arg0, String arg1, Object arg2) {
if (arg1.equals("id") || arg1.equals("serialNumber") || arg1.equals("productName")) {
return false;
} else {
return true;
}
}
});
解决延迟加载产生异常的问题(net.sf.json.JSONException:java.lang.reflect.InvocationTargetException)
JsonConfig config = new JsonConfig();
// 解决延迟加载产生异常的问题
config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });
解决数据库查询结果中,Date转换的问题(net.sf.json.JSONException:java.lang.reflect.InvocationTargetException)
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessor() {
@Override
public Object processArrayValue(Object obj, JsonConfig jsonconfig) {
return null;
} @Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if (value == null)
return "";
// 注意:在判断几个父子级类型时要先判断子类型再判断父类型
if (value instanceof java.sql.Date) {
String str = DateFormat.getDateInstance(DateFormat.DEFAULT).format(value);
return str;
} else if (value instanceof java.sql.Timestamp || value instanceof java.util.Date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String str = format.format(value);
return str;
}
return value.toString();
}
});
有些字段的类型是枚举类型,可以在转换的时候将值设置为枚举类的value或者是label;
首先创建一个枚举类,这里我觉得我该写一篇关于枚举的博客,虽说不咋用。。。。。。
public enum Gender {
// 通过括号赋值,而且必须带有一个参构造器和一个属性跟方法,否则编译出错
// 赋值必须都赋值或都不赋值,不能一部分赋值一部分不赋值;如果不赋值则不能写构造器,赋值编译也出错
MAN("MAN"), WOMEN("WOMEN");
private final String value;
// 构造器默认也只能是private, 从而保证构造函数只能在内部使用
Gender(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
配置config
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(Gender.class, new JsonValueProcessor() {
@Override
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if (value instanceof Gender) {
Gender tmpValue = (Gender) value;
return tmpValue.getValue();
}
return value.toString();
} @Override
public Object processArrayValue(Object arg0, JsonConfig arg1) {
// TODO Auto-generated method stub
return null;
}
});
在处理json的时候可能有提供一些封装好的API,但是掌握基础是很重要的。。。。。。。
java处理json与对象的转化 递归的更多相关文章
- Java中net.sf.json包关于JSON与对象互转的问题
在Web开发过程中离不开数据的交互,这就需要规定交互数据的相关格式,以便数据在客户端与服务器之间进行传递.数据的格式通常有2种:1.xml:2.JSON.通常来说都是使用JSON来传递数据.本文正是介 ...
- Java中net.sf.json包关于JSON与对象互转的坑
在Web开发过程中离不开数据的交互,这就需要规定交互数据的相关格式,以便数据在客户端与服务器之间进行传递.数据的格式通常有2种:1.xml:2.JSON.通常来说都是使用JSON来传递数据.本文正是介 ...
- java对象与Json字符串之间的转化(fastjson)
1. 首先引入jar包 在pom.xml文件里加入下面依赖: <dependency> <groupId>com.alibaba</groupId> <art ...
- java对象与Json字符串之间的转化
public class Test { public static void main(String[] args) { // 实现java对象与Json字符串之间的转化 // 1. Person对象 ...
- Java中JSON字符串与java对象的互换实例详解
这篇文章主要介绍了在java中,JSON字符串与java对象的相互转换实例详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JS ...
- (转)Java中JSON字符串与java对象的互换实例详解
在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,JSON在这方面则做的很好, ...
- -java转json hibernate懒加载造成的无限递归问题
1.在判断到底是谁维护关联关系时,可以通过查看外键,哪个实体类定义了外键,哪个类就负责维护关联关系. JoinColumn(name="pid") 2. 在保存数据时,总是先保存的 ...
- Java基础/利用fastjson反序列化json为对象和对象数组
利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...
- JSON数组对象和JSON字符串的转化,map和JSON对象之间的转化
这种用法包括前端和后端: 前端: 1. 转化为JSON对象方便操作 var jsonObj = JSON.parse(str); 得到的是一个json数组对象,可以通过 for (var p in j ...
随机推荐
- move_uploaded_file
move_uploaded_file() 函数将上传的文件移动到新位置. 若成功,则返回 true,否则返回 false. 语法 move_uploaded_file(file,newloc) 参数 ...
- Python3 的序列
序列 1.根据列表.元组.字符串的共同点把它们统称为序列(他们都是兄弟呀) 1)都可以通过索引来的到每一个元素 2)默认索引值都是从零开始(Python也支持负数索引) 3)都可以通过分片(切片)的方 ...
- eclipse中如何同期化
打开MyEclipse8.0help->Software Updates->find and install(如果没有这个就用help->Software Updates->A ...
- uwsgi常用配置
一.安装方式 1.wget 可以去官网:https://pypi.python.org/pypi/uWSGI/ 下载对应的版本 tar -xvf uwsgi-2.13.1.tar.gz cd uw ...
- hashCode方法和equals方法比较
为什么用HashCode比较比用equals方法比较要快呢?我们要想比较hashCode与equals的性能,得先了解HashCode是什么. HashCode HashCode是jdk根据对象的地址 ...
- jquery判断数据类型和相同字符串不相等
typeof object返回object对象数据类型 encodeURIComponent(str)//可把字符串作为URI 组件进行编码. 若str1和str2字符串数值相同,encodeURIC ...
- Request.getparameternames 获取form表单里面所有的请求参数 。 返回一个Enumeration类型的枚举.
通过Enumeration的hasMoreElements()方法遍历.再由nextElement()方法获得枚举的值.此时的值是form表单中所有控件的name属性的值. 最后通过request.g ...
- Ubuntu中使用dnw工具:没有找到/dev/secbulk0
Ubuntu中使用dnw动机: 一. 之前没有用ubuntu中的dnw,想试试. 二. 因为换了win10系统,怕搞不定win10中dnw的驱动,想着在ubuntu中不用禁用数字签名啥的比较省心.(事 ...
- 【转】matlab图形句柄详解(一)
在matlab中,每一个对象都有一个数字来标识,叫做句柄.当每次创建一个对象时,matlab就为它建立一个唯一的句柄,句柄中包含有该对象的相关信息参数,可以在后续程序中进行操作,改变其中的参数,以便达 ...
- Linux命令:useradd
Linux下:useradd 等价于 adduser Aix下:useradd 来自为知笔记(Wiz)