Wednesday, 15 September 2010

Convert JSON object to Java bean using org.json package -



Convert JSON object to Java bean using org.json package -

i have java bean class, example:

public class user implements serializable{ protected string name = null; protected string password = null; // ... }

i can convert org.json object using

user u = new user(); u.setname("aaa"); u.setpassword("123"); jsonobject jo = new jsonobject(u);

is way convert jsonobject java bean class?

there no built-in way using json.org library.

depending on needs, can either

write fromjsonobject() method each of beans, uses jsonobject#has() , jsonobject#get*() needed values , handle type problems. write global method uses jsonobject#names() , reflection populate bean instance info jsonobject. not difficult, heavy lifting if need utilize couple of bean classes.

java json

No comments:

Post a Comment