joshjdevl
joshjdevl

Reputation: 7232

what is the easiest way to convert / map a JSON string to a javabean?

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?

Upvotes: 4

Views: 1935

Answers (3)

cliff.meyers
cliff.meyers

Reputation: 17734

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

http://x-stream.github.io/json-tutorial.html

Upvotes: 2

StaxMan
StaxMan

Reputation: 116610

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.

Upvotes: 2

fforw
fforw

Reputation: 5491

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.

Upvotes: 2

Related Questions