title | description | created | updated | color |
---|---|---|---|---|
Gson (Java Library) |
Gson is a Java Library which is used to convert Java Objects to JSON strings and back. Gson library provides toJson() and fromJson() methods using which users can serialize /deserialize Java objects to and back from JSON Strings |
2019-06-25 |
2019-06-25 |
Gson is a Java Library which is used to convert Java Objects to JSON strings and back. Gson library provides toJson()
and fromJson()
methods using which users can serialize /deserialize Java objects to and back from JSON Strings.
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
public class GsonObjectToJSONExample {
public static void main(String[] args) {
Gson gson = new Gson();
Map<String, Object> javaObject = new HashMap<>();
javaObject.put("aNumber", 1);
javaObject.put("aString", "foo");
javaObject.put("aBoolean", true);
javaObject.put("aList", Arrays.asList("red", "green", "blue"));
String jsonString = gson.toJson(javaObject);
System.out.println(jsonString);
}
}
{"aNumber":1,"aBoolean":true,"aString":"foo","aList":["red","green","blue"]}
import java.util.Map;
import com.google.gson.Gson;
public class GsonJsonToObjectExample {
public static void main(String[] args) {
Gson gson = new Gson();
String jsonString = "{\"aNumber\":1,\"aBoolean\":true,\"aString\":\"foo\",\"aList\":[\"red\",\"green\",\"blue\"]}";
Map javaObject = gson.fromJson(jsonString, Map.class);
System.out.println("aList: " + javaObject.get("aList"));
System.out.println("aNumber: " + javaObject.get("aNumber"));
}
}
aList: [red, green, blue]
aNumber: 1.0