Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created August 17, 2023 18:12
Show Gist options
  • Save Da9el00/6b04869921b87cc0b4647773c95c2336 to your computer and use it in GitHub Desktop.
Save Da9el00/6b04869921b87cc0b4647773c95c2336 to your computer and use it in GitHub Desktop.
How to convert JSON to Java objects using Gson
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
String json = """
{'name': 'Dani',
'age': 45,
'phone': 222333,
'city':'NewYork'}
""";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user);
}
}
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment