Gson 转换树型结构的实例
来源:互联网 发布:rsync ssh 端口号 编辑:程序博客网 时间:2024/06/06 04:04
- 构建树型结构的实体
树型结构的实体主要是用组合模式构建的,且是透明模式的组合模式;关于组合模式,此处不再赘述。
代码部分:
> package com.dp.composite.example4; import com.google.gson.annotations.Expose; import java.io.Serializable; import java.util.ArrayList;/** * @desc 抽象的缺省类 * @creator caozhiqing * @data 2015/8/12 */public abstract class Component implements Serializable{ private transient Component parent; public Component getParent() { return parent; } public void setParent(Component parent) { this.parent = parent; } public void addChild(Component component){ throw new UnsupportedOperationException("不支持此方法"); } public void getChild(int index) { throw new UnsupportedOperationException("不支持此方法"); } public ArrayList<Component> getChild() { throw new UnsupportedOperationException("不支持此方法"); } public void removeChile(Component component) { throw new UnsupportedOperationException("不支持此方法"); } public abstract void operation(String pre);}
package com.dp.composite.example4;import java.util.ArrayList;import java.util.Iterator;/** * @desc 具体树枝类,即容器类 * @creator caozhiqing * @data 2015/8/12 */public class Composite extends Component { ArrayList<Component> children ; private String name; public Composite(String name){ this.name= name; } @Override public void addChild(Component component) { if(component == null){ return; } if(children == null){ children = new ArrayList<Component>(); } children.add(component); component.setParent(this); } @Override public void removeChile(Component component) { if(component == null || children == null){ return; } Iterator<Component> iterator = children.iterator(); while (iterator.hasNext()){ Component component1 = iterator.next(); if(component1 != null && component1 == component){ iterator.remove(); if(component instanceof Composite && component1.getChild() != null){ for(Component component2:component.getChild()){ component2.setParent(this); addChild(component2); } } break; } } } @Override public ArrayList<Component> getChild() { return children; } @Override public void operation(String pre) { System.out.println(pre+"#"+this.name); pre +=" "; if(children != null && children.size()>0){ for(Component component:children){ component.operation(pre); } } }}
package com.dp.composite.example4;/** * @desc 叶子类,即没有子节点 * @creator caozhiqing * @data 2015/8/12 */public class Leaf extends Component { private String name; public Leaf(String name){ this.name = name; } @Override public void operation(String pre) { System.out.println(pre+"-"+this.name); }}
package com.dp.composite.example4;
import com.google.gson.Gson;
/**
* @desc 客户端
* @creator caozhiqing
* @data 2015/8/12
*/
public class Client {
public static void main(String[] arg){
Component root = new Composite(“服装”);
Component man = new Composite(“男装”);
Component wom = new Composite(“女装”);
root.addChild(man); root.addChild(wom); Component leaf1 = new Leaf("夹克"); Component leaf2 = new Leaf("寸衫"); man.addChild(leaf1); man.addChild(leaf2); Component leaf3 = new Leaf("短裙"); Component leaf4 = new Leaf("Browse"); wom.addChild(leaf3); wom.addChild(leaf4); root.operation(""); System.out.println("----------->");
// root.removeChile(man);
// root.operation(“”);
Gson gson = new Gson();
System.out.println(gson.toJson(root));
}
}
“`
解析
此处最主要的是忽略每个子节点的父节点。
即在Component 中把此属性设置为transient 即不被序列化,且不能使用Gson提供的@Expose(serialize = false) 或@Expose(与new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() )配合使用。JSON 结果。
{“children”:[{“children”:[{“name”:”夹克”},{“name”:”寸衫”}],”name”:”男装”},{“children”:[{“name”:”短裙”},{“name”:”Browse”}],”name”:”女装”}],”name”:”服装”}
- 总结:
如果古加入transient关键字,会报栈溢出的bug
这回crm系统封装解析json和以树型为结构的json解析很有帮助。
- Gson 转换树型结构的实例
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- Google Gson的使用方法,实现Json结构的相互转换
- 利用Gson转换Json实例
- Json转换利器Gson实例
- Android的Gson的使用方法,实现Json结构间相互转换
- Android的Gson的使用方法,实现Json结构间相互转换
- Gson转换的一些方式
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
- iOS语音播放之切换听筒和扬声器
- PHP 常用的代码片段
- OpenPR开源代码项目(中科院自动化所)-含很多图像处理、模式识别、机器学习、自然语言处理的算法
- jQuery获得页面元素的绝对/相对位置
- MySQL中TEXT与BLOB类型的区别
- Gson 转换树型结构的实例
- ssh远程登录到另一台机器命令
- 代码规范问题总结(一)
- phpstrom快捷键
- Redis与Memcached的比较
- MyEclipse 2014 Customize Perspective无法打开解决方案
- HDU 5379 Mahjong tree (详解,构造+思维)
- hdu 3416 Marriage Match IV (最短路+最大流)
- poj 1743 Musical Theme