Reputation: 445
Hi I have a json input file as follows,
{'Latitude':'20',
'coolness':2.0,
'altitude':39000,
'pilot':{'firstName':'Buzz',
'lastName':'Aldrin'},
'mission':'apollo 11'}
How to create a java object from the json input file.
Thanks
Upvotes: 0
Views: 1454
Reputation: 55866
There are more than one APIs that can be used. The simplest one is JSONObject
Just do the following:
JSONObject o = new JSONObject(jsonString);
int alt = o.getInt("altitude");
....
there are getXXX
methods for each type. It basically stores the object as a map. This is a slow API.
You may use Google's Gson
, which is an elegant and better library -- slightly more work required than JSONObject. If you are really concerned about speed, use Jackson
.
Upvotes: 1
Reputation: 29267
You can use the very simple GSON library, with the Gson#fromJson() method.
Here's an example: Converting JSON to Java
Upvotes: 1