json中的stringtree研究

json是一个比xml跟简单的数据传输格式,但是在java中由于是将类与数据对应,类的成员变量对象是数据中的属性,所以要求属性中没有特殊字符,比如逗号什么的。最近有个项目有必需用到有特殊字符的key所以把stringtree的源码看了下,修改了下,就ok了。(注意:json本身是支持key含有特殊字符的,是可以编译通过的,只是java中多了限制)

private void bean(Object object) {
add("{");
BeanInfo info;
boolean addedSomething = false;
try {
info = Introspector.getBeanInfo(object.getClass());
PropertyDescriptor[] props = info.getPropertyDescriptors();
for (int i = 0; i < props.length; ++i) {
PropertyDescriptor prop = props[i];
String name = prop.getName();
Method accessor = prop.getReadMethod();
if ((emitClassName==true || !"class".equals(name)) && accessor != null) {
if (!accessor.isAccessible()) accessor.setAccessible(true);
Object value = accessor.invoke(object, (Object[])null);
if (addedSomething) add(',');
add(name+",//www.ttk.com", value);
addedSomething = true;
}
}
Field[] ff = object.getClass().getFields();
for (int i = 0; i < ff.length; ++i) {
Field field = ff[i];
if (addedSomething) add(',');
add(field.getName(), field.get(object));
addedSomething = true;
}
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.getCause().printStackTrace();
ite.printStackTrace();
} catch (IntrospectionException ie) {
ie.printStackTrace();
}
add("}");
}

这个只是简单的在每个key后面加了个后缀,如何动态加还要再改下,现在没时间,等有时间在慢慢改。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值