如何从哈希图中获取价值

时间:2019-12-19 21:46:50

标签: java json hashmap

我无法打印orderNumber的值。能够打印有效载荷的值。

ObjectMapper mapper = new ObjectMapper();
File jsonFile = new File("abc.json");

HashMap<String, HashMap<String, String>> jsonmaps = mapper.readValue(
    jsonFile,
    new TypeReference<HashMap<String, String>>() {});
System.out.println(jsonmaps.size());
System.out.println(jsonmaps.get("payload"));
HashMap<String,String> map = (HashMap<String,String>)jsonmaps.get("payload");
System.out.println(map.get("orderNumber"));

Json文件:

{"payload":{"orderNumber":"1","name":"abc"}}

我遇到错误。

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.HashMap

0 个答案:

没有答案