我的代码中有一个 mongo 数据库调用。来自数据库的响应使用 codehaus jackson 进行映射。杰森:[ { "_id": "555", "rates": 1, "reviews": [ { "author_name": "Instructor 9999", "_authKey": "demo\\556", "text": "asdfa", "date": 551, "_id": "5454-4920", "title": "asdf", "comments": [] } ], "votedUsers": [ { "mng\\39999": 4 } ], "rating": 4 }, { "_id": "45589", "rates": 1, "reviews": [ { "author_name": "feef", "_authKey": "ad\\ads", "text": "Working perfect", "date": 1498659163, "_id": "asdas-319", "title": "test", "comments": [] } ], "votedUsers": [ { "abc\\bis@cdf.com": 4 } ], "rating": 4 }]我创建了以下 DTO 结构:@JsonIgnoreProperties(ignoreUnknown = true)public class MaterialReviewsDTO implements Serializable { private static final long serialVersionUID = 1L; private String _id; private int rates; private List<ReviewsDTO> reviews; private List<VotedUsersDTO> votedUsers; //List<TypeReference<HashMap<String, String>>> votedUsers; private int rating;. //Getter Setter}@JsonIgnoreProperties(ignoreUnknown = true)public class VotedUsersDTO implements Serializable { private static final long serialVersionUID = 1L; private Map<String, String> votedUser; //Getter Setter}下面是我触发查询的代码:List<MaterialReviewsDTO> materialReviewsDTOs = DBConnectionRealmByDBName .find(query, MaterialReviewsDTO.class, CollectionNameConstant.REVIEWS_COLLECTION);问题是除了以下部分之外,所有 JSON 都在 DTO 中映射:"votedUsers" : [ { "abc\\bis@cdf.com" : 4 } ]VotedUserDTO 响应为空。VotedUsers 是包含键值对中数据的对象列表。我没有提到ReviewsDTO,因为它被完美地映射了。我怎样才能映射votedUsers部分? 注意:我正在使用 Spring 进行开发。
3 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
JSON 中的一些观察结果
1. Json 在设计时应牢记固定键和变量值。
2. 由于在上述情况下 Key 和 values 都是可变的,我们可以使用 Map
所以最终的解决方案是
从更改private List<VotedUsersDTO> votedUsers;为private List<Map<String, Integer>> votedUsers
回首忆惘然
TA贡献1847条经验 获得超11个赞
votedUsers 应该是 VotedUsersDTO 列表。
如果您查看 JSON 中的 VotedUsersDTO:
{
"abc\\bis@cdf.com" : 4
}
这意味着有一个字段abc\\bis@cdf.com您希望值为 4。这不符合DTO 定义中的idorvotedUser映射。
添加回答
举报
0/150
提交
取消
