使用@json注解将json对象Map到Map或列表

gstyhher  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(260)

我有以下课程: reactions 不应仅反序列化 keys and values 是我添加的东西,值将Map到它们,因为它们不能Map到Map

public class Exercise implements Serializable {
         .
         .
         .
//I also tried it without @JsonProperty
    @JsonProperty(value= "react_counts", access = JsonProperty.Access.WRITE_ONLY)
//also tried  private Map<String, Integer> reactions;
    private HashMap<String, Integer> reactions;

    private List<String> keys;
    private List<Integer> values;

         .
         .
         .
 @JsonSetter("react_counts")
    public void setReactions(JSONObject reactionMap) throws JSONException {
       //first I tried this 
        reactions=new HashMap<String, Integer>();
        reactions.put("up", reactionMap.getInt("up"));
        reactions.put("down", reactionMap.getInt("down"));
        reactions.put("favorite", reactionMap.getInt("favorite"));

       // then I tried this        
        keys=new ArrayList<String>();
        values= new ArrayList<Integer>();
        values.add(reactionMap.getInt("Up"));

    }

我正在从服务器接收以下json:

{
        "id": 1,
        "duration": 2,
        "status": "",
        "forum": "",
          .
          .
          .

        "react_counts": {
            "up": 1,
            "down": 1,
            "favorite": 0
        }
    }

一切都很好除了 "react_counts" 有人知道为什么吗?

Exercise e = client.getExerciseSync(1);

System.out.println(e.getReactions());

输出:

null

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题