Gson 转换树型结构的实例
2015-08-13 10:01
579 查看
构建树型结构的实体
树型结构的实体主要是用组合模式构建的,且是透明模式的组合模式;关于组合模式,此处不再赘述。
代码部分:
package com.dp.composite.example4;
import com
4000
.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.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解析很有帮助。
树型结构的实体主要是用组合模式构建的,且是透明模式的组合模式;关于组合模式,此处不再赘述。
代码部分:
> 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
4000
.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解析很有帮助。
相关文章推荐
- 架构纵横谈之二 ---- 架构的模式与要点
- 注册表的组织结构
- PostgreSQL教程(二):模式Schema详解
- 代理区独立模式的说明
- SQLSERVER的非聚集索引结构深度理解
- 调整SQLServer2000运行中数据库结构
- C#中的递归APS和CPS模式详解
- C#迭代器模式(Iterator Pattern)实例教程
- C#基础语法:结构和类区别详解
- 深入c# 类和结构的区别总结详解
- c#结构和类的相关介绍
- C#中结构(struct)的部分初始化和完全初始化实例分析
- C#观察者模式(Observer Pattern)实例教程
- C#中类与结构的区别实例分析
- C#枚举类型与结构类型实例解析
- C#单例模式(Singleton Pattern)实例教程
- javascript实现表现、结构、行为分离的选项卡效果!
- Erlang中的模块与模式匹配介绍
- C#装饰器模式(Decorator Pattern)实例教程
- 动态代理的5模式使用示例和Mixin模式