How to convert JSON to Map in java using com.google.gson.Gson in java

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 googleGsonnew 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> mapgoogleGson.fromJson(jsonStringnew TypeToken<Map<String, Object>>() {
          }.getType());

          // Iterate on Map
          map.forEach((kv) -> System.out.println("key = "k" , value = "v));

    }

}

// output
/*
key = empId , value = 11.0
key = name , value = ankit
key = address , value = [ParisLondon]
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 >



3. Simple  (simple.JSONObject)



4. Google Gson - processing java json


eEdit
Must read for you :