Tuesday, 14 May 2013

Convert Java Object To/From JSON (Gson)

How To Convert Java Object To/From JSON (Gson)


Gson is google library for internal use, but now you can use publically.
It is a lightweight data-interchange format.
Gson is easy to learn and implement, We need to know two methods given below.
1 : toJson() – Convert Java object to JSON format.
2 : fromJson() – Convert JSON into Java object.
A pojo, with initialized values, Later use Gson to convert this object to/from JSON formatted string.

toJson() Example:
Convert object to JSON string, and save it as test.json

fromJson() Example: Read data from test.json , convert back to object.

No comments:

Post a Comment