以下对我有用:
Map<String, String> propertyMap = getJsonAsMap(json);
wheregetJsonAsMap
定义如下:
public HashMap<String, String> getJsonAsMap(String json)
{
try
{
ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
HashMap<String, String> result = mapper.readValue(json, typeRef);
return result;
}
catch (Exception e)
{
throw new RuntimeException("Couldnt parse json:" + json, e);
}
}
请注意,如果您的 json 中有子对象(因为它们不是 a ,它们是 another ),这将失败,但如果您的 json 是如下属性的键值列表,则它将起作用:String
HashMap
{
"client_id": "my super id",
"exp": 1481918304,
"iat": "1450382274",
"url": "http://www.example.com"
}