【问题标题】:Changing two filed names during JSON deserialization in JAVA在 JAVA 中的 JSON 反序列化期间更改两个字段名称
【发布时间】:2020-06-21 21:26:37
【问题描述】:

我有一个 json 文件

{"apple" : [
        {
            "first"    : 3,
            "second"    : 5,

        }
      ],
"orange" : [
        {
            "fst"    : 7,
            "snd"    : 8,

        }
      ],
}

反序列化的辅助类


public class Helper {

    private int num1;

    private int num2;

    public Helper(Helper other) {
        this.num1 = other.num1;
        this.num2 = other.num2;
    }

    public int getNum1() {
        return this.num1;
    }

    public int getNum2() {
        return this.num2;
    }
}

一个用于反序列化的 java 类,我需要将其 JSON 键更改为与 Helper 类的实例名称兼容。

public class MyDesClass {

    @SerializedName(value = "apple.first", alternate = "apple.num1")
    @SerializedName(value = "apple.seconds", alternate = "apple.num2")
    private final Helper[] apple;

    public MyDesClass(Helper[] apple) {
        this.apple = apple;
    }
    
}          

而且,在 main.java 中我也有:

/* .... */
        Reader f = new FileReader( PATH_TO_THE_JSON_FILE);
        Gson gson = new Gson();
        GameBoard gameBoard = gson.fromJson(f, MyDesClass.class);
/* .... */

我的问题是如何在MyDesClass 中同时转换两个值(例如firstsecond)(例如此处为num1num2)?目前,我收到SerializedName is not a repeatable annotation type 错误。

【问题讨论】:

    标签: java json gson json-deserialization


    【解决方案1】:

    您需要在 Helper 类中使用 @SerializedName(如果需要,请更新 MyDesClass):

    public class Helper {
    
        @SerializedName(value = "first" alternate={"num1","fst"})
        private int num1;
    
        @SerializedName(value = "second" alternate={"num2","snd"})
        private int num2;
    
        public Helper(Helper other) {
            this.num1 = other.num1;
            this.num2 = other.num2;
        }
    
        public int getNum1() {
            return this.num1;
        }
    
        public int getNum2() {
            return this.num2;
        }
    }
    

    为了符合更新的JSON,MyDesClass需要有另一个字段orange

    public class MyDesClass {
    
        @SerializedName("apple")
        private final Helper[] apple;
    
        @SerializedName("orange")
        private final Helper[] orange;
    
        public MyDesClass(Helper[] apple, Helper[] orange) {
            this.apple = apple;
            this.orange = orange;
        }
    // ...    
    } 
    

    【讨论】:

    • 感谢您的解决方案。但是,JSON 键可以是可变的。抱歉,如果不清楚。我在示例中扩展了 JSON 文件以进行说明。那么,如果Helper类中的反序列化是唯一的选择,那么有没有办法让@SerializedName中的value键变量?
    • 我为您更新的 JSON 更新了响应 - 它需要有另一个字段 orange。但是,您可能打算将 @SerializedName 应用于您的 apple 字段。或者使用<String, List<Helper>> 的映射而不是MyDesClass
    • 非常感谢您的回答,它解决了我的大部分问题。只是,这里还有一个问题。在我原来的问题中,JSON 中的第一个键是一个对象,第二个键是一个对象数组。在 JSON 中,大多数条目的顺序为 key1: Object, key2:[Object1, ...]。但是,在某些情况下,顺序是相反的key1: [Object1, ...], key2:Object,并且这些条目不会被反序列化。有解决此问题的想法吗?
    • 您可能需要检查an answer here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2020-08-27
    • 2014-08-05
    • 2019-02-22
    • 1970-01-01
    • 2016-03-17
    • 2017-09-01
    相关资源
    最近更新 更多