将JSONString反序列化为包含字符串列表的Java对象时出现问题
我有一个JSONString,如下所示:将JSONString反序列化为包含字符串列表的Java对象时出现问题,java,json,Java,Json,我有一个JSONString,如下所示: {"items":[{"attribute":"grade","values":["AA"]},{"attribute":"subject","values":["mathematics"]}]} @Builder @Getter @Setter @ToString public class Marks { @JsonProperty("attribute") private String attributeName; List<Strin
{"items":[{"attribute":"grade","values":["AA"]},{"attribute":"subject","values":["mathematics"]}]}
@Builder
@Getter
@Setter
@ToString
public class Marks {
@JsonProperty("attribute")
private String attributeName;
List<String> values;
}
我创建了一个类标记,如下所示:
{"items":[{"attribute":"grade","values":["AA"]},{"attribute":"subject","values":["mathematics"]}]}
@Builder
@Getter
@Setter
@ToString
public class Marks {
@JsonProperty("attribute")
private String attributeName;
List<String> values;
}
似乎您正试图将其转换为
标记
对象,但项
包含一个标记
数组 能否尝试在属性值声明之前添加@JsonProperty(“值”)
public class Marks {
@JsonProperty("attribute")
private String attributeName;
@JsonProperty("values")
List<String> values;
}
公共类标记{
@JsonProperty(“属性”)
私有字符串attributeName;
@JsonProperty(“值”)
列表值;
}
您的Json字符串有效,但它不是Json数组
能否将Json数组更改为以下格式并再次检查
[{"attribute":"grade","values":["AA"]},{"attribute":"subject","values":["mathematics"]}]
试试下面的
String actionPriorityConfig= "[{\"attribute\":\"grade\",\"values\":[\"AA\"]},{\"attribute\":\"subject\",\"values\":[\"mathematics\"]}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Marks> markList = objectMapper.readValue(actionPriorityConfig, new TypeReference<Marks>());
String actionPriorityConfig=“[{\'属性\':\'等级\',\'值\':[\'AA\']},{\'属性\':\'科目\',\'值\':[\'数学\']}];
ObjectMapper ObjectMapper=新的ObjectMapper();
List markList=objectMapper.readValue(actionPriorityConfig,new TypeReference());
它只在SagarRohankar有效