LoginSignup
0
0

json文字列をMapに変換する方法

Posted at
変換.json
{
  "name": "John",
  "age": 30,
  "address": {
    "street": "1234 Elm St",
    "city": "Somewhere",
    "zip": "12345"
  },
  "phoneNumbers": [
    {"type": "home", "number": "123-456-7890"},
    {"type": "work", "number": "098-765-4321"}
  ]
}
Main.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // JSON文字列の定義(ネストされた構造を含む)
        String json = "{\"name\":\"John\", \"age\":30, \"address\":{\"street\":\"1234 Elm St\", \"city\":\"Somewhere\", \"zip\":\"12345\"}, \"phoneNumbers\":[{\"type\":\"home\", \"number\":\"123-456-7890\"}, {\"type\":\"work\", \"number\":\"098-765-4321\"}]}";

        ObjectMapper mapper = new ObjectMapper();
        try {
            // JSON文字列をMapに変換
            Map<String, Object> map = mapper.readValue(json, Map.class);

            // 変換結果のMapを出力
            System.out.println("Converted Map: " + map);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0