Maven dependency -
<!-- 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>
|
If you are beginner don't worry learn how to Create new Maven project - In 2 minutes
How to convert JSON to Map in java using com.google.gson.Gson in java
package com.ankit.json;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class ConvertJSONToMap {
public static void main(String[] args) {
Gson googleGson = new Gson();
String jsonString = "{\"empId\":11,\"name\":\"ankit\",\""
+ "address\":[\"Paris\",\"London\"],\"birthDate\":\"Sep 2, 2018 12:59:30 PM\"} ";
//How to convert JSON to Map in java
Map<String, Object> map = googleGson.fromJson(jsonString, new TypeToken<Map<String, Object>>() {
}.getType());
// Iterate on Map
map.forEach((k, v) -> System.out.println("key = " + k + " , value = " + v));
}
}
// output
/*
key = empId , value = 11.0
key = name , value = ankit
key = address , value = [Paris, London]
key = birthDate , value = Sep 2, 2018 12:59:30 PM
*/
|
Related links >
1. Jackson JSON -
We can use Jackson api for for processing JSON in java.
Jackson JSON examples
2. Java provides API (JSR 353) for Processing JSON (JSR 353).
It provides -
- Object Model API
- Streaming API
Java API for JSON processing examples >