Creating JSON with Gson in Java


Gson is another popular library for dealing with JSON in Java. It’s known for its ease of use and flexibility.

Source Code

// Assuming Gson is added to your project
Gson gson = new Gson();
Map personMap = new HashMap();
personMap.put("name", "John");
personMap.put("age", 30);
String json = gson.toJson(personMap);
System.out.println(json);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments