javaでJSONをマップに変換する方法は何ですか。

Jacksonライブラリを使用してJSONをMapに変換することができます。

こちらはサンプルコードです:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Map;

public class JsonToMapExample {
    public static void main(String[] args) {
        String json = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";

        try {
            ObjectMapper objectMapper = new ObjectMapper();

            // 将JSON字符串转换成Map
            Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>(){});

            // 打印Map的内容
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上記のコードを実行すると、出力結果は次のとおりです:

name: John
age: 30
city: New York

JSON文字列がMapオブジェクトに変換されました。

bannerAds