Employers: discover CodinGame for tech hiring
Practice
Compete
1
Contribute
Learn
2
Log In
Sign Up
👨🎓 How to parse JSON in Java
[CG]BOUGA
21.5K views
Using
Gson
JSON.java
User.java
1
10
11
12
13
14
15
16
17
18
19
20
21
26
// {
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);
// {
1
2
3
4
public
class
User {
int
id;
String name;
}
Run
2
0
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
Suggested playgrounds
How to test if a List contains a specific value in Java
By
[CG]BOUGA
24K
8
1
How to encode/decode in base64 in Java
By
[CG]BOUGA
8,884
1
0
Java 8 Stream map tutorial
By
Unnamed contributor
3,527
12
0
Reactive Programming with Reactor 3
By
Reactor
299.5K
286
32
CodinGame - Home
ABOUT US
CAREERS
PRIVACY
FAQ
CodinGame - Work
The #1 tech hiring platform
SCREENING
SOURCING
RETAIN
ABOUT US
CAREERS
PRIVACY
FAQ
Facebook
YouTube
Twitter
LinkedIn
FR
EN
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants
Keyboard Shortcuts:
?
Show / hide this help menu
×