Home > Java > javaTutorial > How can I convert a JSON String to a HashMap in Java?

How can I convert a JSON String to a HashMap in Java?

Mary-Kate Olsen
Release: 2024-11-11 22:45:03
Original
214 people have browsed it

How can I convert a JSON String to a HashMap in Java?

Converting a JSON String to a HashMap in Java

In Java, you can encounter scenarios where you need to store JSON data in a structured and accessible data format. One convenient option for this is to use a HashMap, which allows you to map keys to their corresponding values.

To convert a JSON String into a HashMap, you can leverage the org.json library. Here's a sample code snippet that demonstrates the process recursively:

public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException {
    Map<String, Object> retMap = new HashMap<>();
    
    if(json != JSONObject.NULL) {
        retMap = toMap(json);
    }
    return retMap;
}

public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap<>();

    Iterator<String> keysItr = object.keys();
    while(keysItr.hasNext()) {
        String key = keysItr.next();
        Object value = object.get(key);
        
        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }
        
        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        map.put(key, value);
    }
    return map;
}

public static List<Object> toList(JSONArray array) throws JSONException {
    List<Object> list = new ArrayList<>();
    for(int i = 0; i < array.length(); i++) {
        Object value = array.get(i);
        if(value instanceof JSONArray) {
            value = toList((JSONArray) value);
        }

        else if(value instanceof JSONObject) {
            value = toMap((JSONObject) value);
        }
        list.add(value);
    }
    return list;
}
Copy after login

In this code, the jsonToMap method initializes a HashMap called retMap and checks if the input JSONObject is not null. If it's not null, it invokes the recursive toMap method to convert the JSONObject to a HashMap.

The toMap method iterates over the keys in the JSONObject and extracts the corresponding values. It recursively handles nested JSON objects and arrays, effectively converting the entire JSON structure into a HashMap.

Alternatively, if you prefer a non-recursive approach and are working with Jackson library, you can use the following code:

import com.fasterxml.jackson.databind.ObjectMapper;

Map<String, Object> mapping = new ObjectMapper().readValue(jsonStr, HashMap.class);
Copy after login

This approach simplifies the conversion process by utilizing the Jackson library's ObjectMapper class, which can directly parse JSON Strings into desired Java objects.

By implementing these techniques, you can effectively convert JSON Strings into HashMaps in Java, providing you with a flexible and efficient data structure for storing and managing JSON data.

The above is the detailed content of How can I convert a JSON String to a HashMap in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template