a problem with json-lib-2.2.2-jdk15.jar?

342 views
Skip to first unread message

rmn190

unread,
Jun 20, 2008, 1:46:37 AM6/20/08
to JSON Schema
When converting form json-formated string to JavaBean, I met the
problem about java.util.Date converting.

the following is the Java file that I tested with:

========================================

package json;

import java.util.Date;

import net.sf.json.JSONObject;

public class Person {
private Date birthday;

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public static Person getInstance(String jsonVale) {
return
(Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
}

public static void main(String[] args) {
String personJson = "{birthday:\"06/28/2008
17:00:00\"}";

Person p = getInstance(personJson);

System.out.println("Person's brithday: "+
p.getBirthday());
}
}


=====================================================
Running the class would report the following message:

2008-6-19 13:57:39 net.sf.json.JSONObject morphPropertyValue
warning: Can not transform property 'birthday' from
java.lang.String into java.util.Date. Will register a default
Morpher
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
Info: Property 'java.util.Date.class' has no write method. SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.date' does not exist. SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
Info: Property 'java.util.Date.day' has no write method. SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.hours' does not exist. SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.minutes' does not exist.
SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.month' does not exist. SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.seconds' does not exist.
SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.time' does not exist. SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
Info: Property 'java.util.Date.timezoneOffset' has no write method.
SKIPPED.
2008-6-19 13:57:39 net.sf.ezmorph.bean.BeanMorpher morph
warning: Property 'java.lang.String.year' does not exist. SKIPPED.
Person's brithday: Thu Jun 19 13:57:39 CST 2008


As the result shows,"Thu Jun 19 13:57:39 CST 2008 " is not equal to
"06/28/2008 17:00:00". I know it may be related to JSONConfig, however
I dont solve it.

Would you please help me with that? Thanks a lot!

Thanks in advance!

rmn190

unread,
Jun 20, 2008, 6:12:16 AM6/20/08
to JSON Schema
public static Person getInstance(String jsonVale) {
return
(Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
}

changed to:

public static Person getInstance(String jsonVale) {
JSONUtils.getMorpherRegistry().registerMorpher(new
DateMorpher(new String[] {"MM/dd/yyyy HH:mm:ss"}) );
return
(Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
}

i.e:
add a sentence of "JSONUtils.getMorpherRegistry().registerMorpher(new
DateMorpher(new String[] {"MM/dd/yyyy HH:mm:ss"}) );" to configure the
Morpher.

:),:) :),:) :),:),:):),:)

rmn190

unread,
Jun 20, 2008, 6:12:16 AM6/20/08
to JSON Schema
public static Person getInstance(String jsonVale) {
return
(Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
}

changed to:

public static Person getInstance(String jsonVale) {
JSONUtils.getMorpherRegistry().registerMorpher(new
DateMorpher(new String[] {"MM/dd/yyyy HH:mm:ss"}) );
return
(Person)JSONObject.toBean(JSONObject.fromObject(jsonVale),Person.class);
}

i.e:
add a sentence of "JSONUtils.getMorpherRegistry().registerMorpher(new
DateMorpher(new String[] {"MM/dd/yyyy HH:mm:ss"}) );" to configure the
Morpher.

:),:) :),:) :),:),:):),:)

On 6月20日, 下午1时46分, rmn190 <rmn...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages