简体   繁体   中英

Unable Parse a JSON using Jackson

I have a mongo database call in my code. The response from the database is mapped using codehaus jackson.

Json:

[
  {
    "_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
  }
]

I have created the below DTO Stucture:

@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
}

Below is the code where I am firing the query:

List<MaterialReviewsDTO> materialReviewsDTOs = DBConnectionRealmByDBName
                    .find(query,
                            MaterialReviewsDTO.class,
                            CollectionNameConstant.REVIEWS_COLLECTION);

Problem is all the JSON is getting mapped in DTO except the below part:

"votedUsers" : [ 
            {
                "abc\\bis@cdf.com" : 4
            }
        ]

VotedUserDTO is null in response. VotedUsers is a list of object containg data in key-value pair.

I am not mentioning ReviewsDTO as this is getting mapped perfectly. How can I map votedUsers part? Note: I am using Spring for development.

Some of the observations from your JSON
1. Json should be designed with Fixed key and variable values in mind.
2. Since in above case both Key and values are variable, we can use Map
So final solution is
change from private List<VotedUsersDTO> votedUsers; to private List<Map<String, Integer>> votedUsers

private List<Map<String, String>> votedUsers;

不要使用显式的votedUser DTO。

The votedUsers is expected to be a List of VotedUsersDTOs.
If you look at your VotedUsersDTO in the JSON:

{
"abc\\bis@cdf.com" : 4
}

This would imply there is a field abc\\\\bis@cdf.com where you want the value to be 4. This doesn't comply with id or votedUser Map in the DTO definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM