Answers for "java object to json string gson"

1

Convert JSON to java objects with gson

Gson gson = new Gson();

	// 1. JSON file to Java object
	Staff staff = gson.fromJson(new FileReader("C:\\projects\\staff.json"), Staff.class);

	// 2. JSON string to Java object
	String json = "{'name' : 'mkyong'}";
	Staff staff = gson.fromJson(json, Staff.class);
		
	// 3. JSON file to JsonElement, later String
	JsonElement json = gson.fromJson(new FileReader("C:\\projects\\staff.json"), JsonElement.class);
    String result = gson.toJson(json);
Posted by: Guest on September-21-2021
0

java gson string to json

JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);
Posted by: Guest on March-31-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language