Home > Java > javaTutorial > How to Convert JSON Strings to HashMaps in Java Using the org.json Library?

How to Convert JSON Strings to HashMaps in Java Using the org.json Library?

Mary-Kate Olsen
Release: 2024-11-13 14:03:02
Original
897 people have browsed it

How to Convert JSON Strings to HashMaps in Java Using the org.json Library?

Converting JSON Strings to HashMaps in Java Using the org.json Library

When working with JSON data in Java, a common task is to convert JSON strings into HashMaps to facilitate data manipulation and storage. This article provides a detailed explanation on how to achieve this conversion using the org.json library.

JSON Structure

Consider the following JSON string:

{
"name" : "abc",
"email id" : ["[email protected]", "[email protected]", "[email protected]"]
}
Copy after login

The goal is to convert this JSON string into a HashMap with keys matching the property names and values corresponding to their respective values.

HashMap Initialization

First, initialize a HashMap to store the converted data:

Map<String, Object> retMap = new HashMap<String, Object>();
Copy after login

Recursive Conversion

The conversion process involves recursively traversing the JSON object and converting nested structures as well. The following code snippet outlines the recursive conversion method:

public static Map jsonToMap(JSONObject json) throws JSONException {
    Map<String, Object> retMap = new HashMap<String, Object>();

    if(json != JSONObject.NULL) {
        retMap = toMap(json);
    }
    return retMap;
}

public static Map toMap(JSONObject object) throws JSONException {
    Map map = new HashMap();

    Iterator 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 toList(JSONArray array) throws JSONException {
    List 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

This code recursively traverses the JSON object and handles nested structures, such as arrays and objects, ensuring that the final HashMap contains all the data in the JSON string.

Using the Jackson Library

Alternatively, you can utilize the Jackson library for converting JSON strings to HashMaps. This can be done with the following code:

import com.fasterxml.jackson.databind.ObjectMapper;

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

The above is the detailed content of How to Convert JSON Strings to HashMaps in Java Using the org.json Library?. 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