在java中将字符串转换为map

时间:2016-06-18 11:29:36

标签: java dictionary objectmapper

我有以下需要转换为map的字符串值,我使用了以下代码

String parameters='[{"key":"login","value":"admin"},{"key":"Createddate","value":"cairo"}]';

ObjectMapper mapper = new ObjectMapper();
Map<String, String> map ;

map = mapper.readValue(parameters, new TypeReference<Map<String, String>>(){});
System.out.println(map);

我收到以下错误

com.fasterxml.jackson.databind.JsonMappingException: Can not
deserialize instance of java.util.LinkedHashMap out of START_ARRAY token
at [Source: [{"key":"login","value":"admin"},
{"key":"Createddate","value":"cairo"}]; line: 1, column: 1]

1 个答案:

答案 0 :(得分:-1)

String param="[{\"key\":\"login\",\"value\":\"admin\"},{\"key\":\"Createddate\",\"value\":\"cairo\"}]";

ObjectMapper mapper = new ObjectMapper();
List maps = mapper.readValue(param, List.class);

for (int i=0 ; i<maps.size() ; i++){
    Log.d("MAP", maps.get(i).toString());
}