Convert JSON to Map
You can use the Gson library to convert a JSON string to a Map in Java.
Here is an example of how you can do this:
import com.google.gson.Gson;
import java.util.Map;
String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
Gson gson = new Gson();
Map map = gson.fromJson(json, Map.class);
System.out.println(map);This will output the following:
{name=John, age=30, city=New York}
Alternatively, you can use the Jackson library to convert a JSON string to a Map. Here is an example of how you can do this using Jackson:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(json, Map.class);
System.out.println(map);This will also output the following:
{name=John, age=30, city=New York}
Note that both Gson and Jackson require adding the respective library as a dependency to your project.