-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGsonFromJson.java
More file actions
36 lines (24 loc) · 816 Bytes
/
GsonFromJson.java
File metadata and controls
36 lines (24 loc) · 816 Bytes
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
27
28
29
30
31
32
33
34
35
36
package com.zetcode;
import com.google.gson.Gson;
class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return new StringBuilder().append("User{").append("First name: ")
.append(firstName).append(", Last name: ")
.append(lastName).append("}").toString();
}
}
public class GsonFromJson {
public static void main(String[] args) {
String json_string = "{\"firstName\":\"Tom\", \"lastName\": \"Broody\"}";
Gson gson = new Gson();
User user = gson.fromJson(json_string, User.class);
System.out.println(user);
}
}