変換.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();
}
}
}