tags:

views:

485

answers:

3

Hi,

I have a JSON string that I read in and would like to parse / map it to a JavaBean so I can use it in my Java code. What is the easiest way / library to achieve this?

+1  A: 

XStream is renowned for its ease of use and supports JSON:

http://xstream.codehaus.org/json-tutorial.html

cliff.meyers
+1  A: 

I wrote a JSON library to do just that..

http://code.google.com/p/svenson/

With svenson you would do something like:

// assume json to be a JSON dataset as String
MyBean bean = JSONParser.defaultJSONParser().parse(MyBean.class, json);

Svenson gives you free choice of either using maps/lists or your own POJOs to convert data to and from JSON.

fforw
A: 

How about Jackson? Like one of the other ones mentioned, mapping is quite simple:

MyBean bean = new ObjectMapper().readValue(json, MyBean.class);

handles Maps, Lists, primitives, beans; with proper generics support and full configurability of mapping process.

StaxMan