Created
August 17, 2023 18:12
-
-
Save Da9el00/6b04869921b87cc0b4647773c95c2336 to your computer and use it in GitHub Desktop.
How to convert JSON to Java objects using Gson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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