Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Using Gson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// {
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class JSON {
public static void main(String[] args) throws Exception {
// }
String json = "{\"name\": \"Bob\", \"id\": \"123\"}";
// Method 1: parsing into a JSON element
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString());
// Method 2: parsing into a Java Object
User user = new Gson().fromJson(json, User.class);
System.out.println(user.id);
// {
}
}
// }
Press desired key combination and then press ENTER.
1
public class User {
Press desired key combination and then press ENTER.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content