我正在尝试使用以下JSON字符串创建另一个值的Map:
"{\"conditionField\":\"myFieldName\",\"conditionValue\":\"myFieldValue\",\"concatenationValues\":[\"fieldValue1\",\"fieldValue2\",\"fieldValue3\"]}"
当我尝试将其转换为POJO时获取以下JsonMappingException:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of JSONConcatination: no String-argument constructor/factory method to deserialize from String value ('{"conditionField":"myFieldName","conditionValue":"myFieldValue","concatenationValues":["fieldValue1","fieldValue2","fieldValue3"]}')
at [Source: "{\"conditionField\":\"myFieldName\",\"conditionValue\":\"myFieldValue\",\"concatenationValues\":[\"fieldValue1\",\"fieldValue2\",\"fieldValue3\"]}"; line: 1, column: 1]
使用以下代码:
private final Map<String, Map<String, List<String>>> jsonResources = new HashMap<String, Map<String, List<String>>>();
ObjectMapper mapper = new ObjectMapper();
JSONConcatination jsonConcatination = mapper.readValue(json, JSONConcatination.class);
Map<String, List<String>> values = new HashMap<String, List<String>>();
values.put(jsonConcatination.getConditionValue(), jsonConcatination.getConcatenationValues());
jsonResources.put(jsonConcatination.getConditionField(), values);
@Data
public class JSONConcatination {
private String conditionField;
private String conditionValue;
private List<String> concatenationValues;
}
答案 0 :(得分:2)
您的JSON字符串无效,首先使用以下替换操作使其成为有效字符串,这将删除json字符串中的开始和结束引号以及反斜杠。 现在杰克逊将此视为有效的json字符串。
json = json.replaceAll("^\"|\"$|\\\\", "");
那么这将有效。