Hi I'm getting exception while invoking JSONObject.fromObject( new MyBean() )
I'm using following set of libs..
\commons-beanutils.jar
\commons-collections-3.2.jar
\commons-lang-2.3.jar
\commons-logging-1.1.1.jar
\ezmorph-1.0.4.jar
\json-lib-2.2-jdk15.zip
\commons-beanutils-bean-collections.jar
\commons-beanutils-core.jar
\junit4.4\junit-4.4.jar
=============================================
package com.visu.binbaz.server.json;
import static net.sf.json.test.JSONAssert.*;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class JsonTest {
public static void main(String[] args) {
MyJavaBean bean = new MyJavaBean();
bean.setString( "JSON" );
bean.setInteger( 1 );
bean.setDooble( 2.0d );
bean.setBool( true );
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean );
assertEquals( "JSON", jsonObject.getString("string") );
assertEquals( 1, jsonObject.getInt("integer") );
assertEquals( 2.0d, jsonObject.getDouble("dooble"), 0d );
assertTrue( jsonObject.getBoolean("bool") );
}
}
=====================================================================
class MyJavaBean {
private String string;
private int integer;
private double dooble;
private boolean bool;
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public int getInteger() {
return integer;
}
public void setInteger(int integer) {
this.integer = integer;
}
public double getDooble() {
return dooble;
}
public void setDooble(double dooble) {
this.dooble = dooble;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
// getters & setters
}
=============================================
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'bool' has no getter method
at net.sf.json.JSONObject._fromBean(JSONObject.java:947)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONArray.fromObject(JSONArray.java:192)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:113)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
at com.visu.binbaz.server.json.JsonTest.main(JsonTest.java:13)
Caused by: java.lang.NoSuchMethodException: Property 'bool' has no getter method
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1127)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
at net.sf.json.JSONObject._fromBean(JSONObject.java:922)
... 5 more
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This behavior is weird, per the exception the problem lies in a PropertyUtils.getProperty() call.
Which version of beanutils are you using?
do you have multiple beanutils jars in your classpath by any chance?
does it fails just for a boolean property or for any property?
is your bean class public?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I got it..
My bean class was not public..now it's working fine .
I'm wondering about the exception,if it's able to pick the class def. it should be able to pick the public property in the class.
Any how it's working great
Thanks aalmiray ..
raghunath nandyala
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well not exactly, even though the class def was available the properties were not, it has something to do with how PropertyUtils works, try the same with a package protected inner bean and you'll see that properties are not available.
If this problem because more common then I'll look for another solution for accessing properties.
Cheers,
Andres
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi I'm getting exception while invoking JSONObject.fromObject( new MyBean() )
I'm using following set of libs..
\commons-beanutils.jar
\commons-collections-3.2.jar
\commons-lang-2.3.jar
\commons-logging-1.1.1.jar
\ezmorph-1.0.4.jar
\json-lib-2.2-jdk15.zip
\commons-beanutils-bean-collections.jar
\commons-beanutils-core.jar
\junit4.4\junit-4.4.jar
=============================================
package com.visu.binbaz.server.json;
import static net.sf.json.test.JSONAssert.*;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class JsonTest {
public static void main(String[] args) {
MyJavaBean bean = new MyJavaBean();
bean.setString( "JSON" );
bean.setInteger( 1 );
bean.setDooble( 2.0d );
bean.setBool( true );
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( bean );
assertEquals( "JSON", jsonObject.getString("string") );
assertEquals( 1, jsonObject.getInt("integer") );
assertEquals( 2.0d, jsonObject.getDouble("dooble"), 0d );
assertTrue( jsonObject.getBoolean("bool") );
}
}
=====================================================================
class MyJavaBean {
private String string;
private int integer;
private double dooble;
private boolean bool;
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public int getInteger() {
return integer;
}
public void setInteger(int integer) {
this.integer = integer;
}
public double getDooble() {
return dooble;
}
public void setDooble(double dooble) {
this.dooble = dooble;
}
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
// getters & setters
}
=============================================
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'bool' has no getter method
at net.sf.json.JSONObject._fromBean(JSONObject.java:947)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONArray.fromObject(JSONArray.java:192)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:113)
at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
at com.visu.binbaz.server.json.JsonTest.main(JsonTest.java:13)
Caused by: java.lang.NoSuchMethodException: Property 'bool' has no getter method
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1127)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
at net.sf.json.JSONObject._fromBean(JSONObject.java:922)
... 5 more
I tried with
public boolean getBool(){..}
public boolean isBool(){..}
But same exception..
This behavior is weird, per the exception the problem lies in a PropertyUtils.getProperty() call.
Which version of beanutils are you using?
do you have multiple beanutils jars in your classpath by any chance?
does it fails just for a boolean property or for any property?
is your bean class public?
I got it..
My bean class was not public..now it's working fine .
I'm wondering about the exception,if it's able to pick the class def. it should be able to pick the public property in the class.
Any how it's working great
Thanks aalmiray ..
raghunath nandyala
Well not exactly, even though the class def was available the properties were not, it has something to do with how PropertyUtils works, try the same with a package protected inner bean and you'll see that properties are not available.
If this problem because more common then I'll look for another solution for accessing properties.
Cheers,
Andres