使用org.json包,new JSONObject(Object obj)后,obj信息在生成的json对象(json字符串)中消失一部分(不完整)

从JSONObject源码注释知:obj应该是一个Bean,其类型必须为public的class,将obj的实际类型【Object的子类,即EventType】改为public的类型后,信息保存完整,问题解决。

 

JSONObject部分源码如下(看注释):

/**
     * Construct a JSONObject from an Object using bean getters. It reflects on
     * all of the public methods of the object. For each of the methods with no
     * parameters and a name starting with <code>"get"</code> or
     * <code>"is"</code> followed by an uppercase letter, the method is invoked,
     * and a key and the value returned from the getter method are put into the
     * new JSONObject.
     *
     * The key is formed by removing the <code>"get"</code> or <code>"is"</code>
     * prefix. If the second remaining character is not upper case, then the
     * first character is converted to lower case.
     *
     * For example, if an object has a method named <code>"getName"</code>, and
     * if the result of calling <code>object.getName()</code> is
     * <code>"Larry Fine"</code>, then the JSONObject will contain
     * <code>"name": "Larry Fine"</code>.
     *
     * @param bean
     *            An object that has getter methods that should be used to make
     *            a JSONObject.
     */
    public JSONObject(Object bean) {
        this();
        this.populateMap(bean);
    }




附录:摘自百度

javaBean的使用规则

1.一个javabean类必须是一个公共类。 2.一个javabean类必须有一个空的构造函数。 3.一个javabean类不应有公共实力变量。 4.持有值应该通过一组存取方法(getXXX和setXXX)必须来访问(如果在使用boolean类型的变量时,用户可以用is代替get();和set();). ... 1.一个javabean类必须是一个公共类。
2.一个javabean类必须有一个空的构造函数。
3.一个javabean类不应有公共实力变量。
4.持有值应该通过一组存取方法(getXXX和setXXX)必须来访问(如果在使用boolean类型的变量时,用户可以用is代替get();和set();).
请大家给我一个javabean例子,要求javabean的规范格式要符合上面4点的要求。
谢谢!
展开
在请加上注释//
谢谢。
LZ2199413 | 浏览 5166 次
推荐于2016-08-28 03:41:39 最佳答案
(1)JavaBean 类必须是一个公共类,并将其访问属性设置为 public  ,如: public class user{......}
(2)JavaBean 类必须有一个空的构造函数:类中必须有一个不带参数的公用构造器
(3)一个javaBean类不应有公共实例变量,类变量都为private  ,如: private int id;
(4)属性应该通过一组存取方法(getXxx 和 setXxx)来访问,一般是IDE(Eclipse、JBuilder) 为属性生成getter/setter 方法
一般JavaBean属性以小写字母开头,驼峰命名格式,相应的 getter/setter 方法是 get/set 接上首字母大写的属性名。例如:属性名为userName,其对应的getter/setter 方法是 getUserName/setUserName。
但是,还有一些特殊情况:
1、如果属性名的第二个字母大写,那么该属性名直接用作 getter/setter 方法中 get/set 的后部分,就是说大小写不变。例如属性名为uName,方法是getuName/setuName。
2、如果前两个字母是大写(一般的专有名词和缩略词都会大写),也是属性名直接用作 getter/setter 方法中 get/set 的后部分。例如属性名为URL,方法是getURL/setURL。
3、如果首字母大写,也是属性名直接用作 getter/setter 方法中 get/set 的后部分。例如属性名为Name,方法是getName/setName,这种是最糟糕的情况,会找不到属性出错,因为默认的属性名是name。
所以在JavaBean命名时应该注意符合以上命名规范。
附下表,看看吧:
属性名/类型                    |                       getter 方法              |                    setter 方法  
xcoordinate/Double     | public Double getXcoordinate()     | public void setXcoordinate(Double newValue)

xCoordinate/Double    | public Double getxCoordinate()     |public void setxCoordinate(Double newValue)

XCoordinate/Double    |public Double getXCoordinate()     | public void setXCoordinate(Double newValue)

Xcoordinate/Double     |不允许                                             |    不允许
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值