Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用嵌套属性反序列化JSON对象_Java_Json_Jackson_Annotations - Fatal编程技术网

Java 使用嵌套属性反序列化JSON对象

Java 使用嵌套属性反序列化JSON对象,java,json,jackson,annotations,Java,Json,Jackson,Annotations,我正在尝试用属性反序列化json对象 其中第一个键名未知,而内部映射始终包含两个名为“key1”和“key2”的属性 我有密码: Operation.java public class Operation { private Map<String, Map<String, Integer>> operationDetails = new HashMap<>(); @JsonAnyGetter public Map<Str

我正在尝试用属性反序列化json对象

其中第一个键名未知,而内部映射始终包含两个名为“key1”和“key2”的属性

我有密码:

Operation.java

public class Operation {
    private Map<String, Map<String, Integer>> operationDetails = new HashMap<>();
    
    @JsonAnyGetter
    public Map<String, Map<String, Integer>> getOperation() {
        return operationDetails;
    }
} 

并将上述映射调整为仅
Map

,您需要一个对象表示来表示预期的JSON和Gson依赖关系

对象的类


    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonRootName;

    @JsonRootName("operation_rand")
    public class OperationRand{
    @JsonProperty("key1")
    private int key1;
    @JsonProperty("key2")
    private int key2;

    public int getKey1() {
        return key1;
    }

    public void setKey1(int key1) {
        this.key1 = key1;
    }

    public int getKey2() {
        return key2;
    }

    public void setKey2(int key2) {
        this.key2 = key2;
    }

    public OperationRand(int key1, int key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    @Override
    public String toString() {
        return "OperationRand{" +
                "key1=" + key1 +
                ", key2=" + key2 +
                '}';
    }
    }

谷歌的格森

    <dependency>

        <groupId>com.google.code.gson</groupId>

        <artifactId>gson</artifactId>

    </dependency>
[编辑]用于更改操作\u 我会将对象用于非更改字段


    import lombok.*;
    
    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class Key {
    int key1;
    int key2;
    }

大体上


    Gson gson = new Gson();
    Map<String,Key> rest = gson.fromJson(json,HashMap.class);
    System.out.println(rest);


Gson Gson=新的Gson();
Map rest=gson.fromJson(json,HashMap.class);
系统输出打印项次(剩余);

对于预期的JSON和Gson依赖项,您需要一个对象表示

对象的类


    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonRootName;

    @JsonRootName("operation_rand")
    public class OperationRand{
    @JsonProperty("key1")
    private int key1;
    @JsonProperty("key2")
    private int key2;

    public int getKey1() {
        return key1;
    }

    public void setKey1(int key1) {
        this.key1 = key1;
    }

    public int getKey2() {
        return key2;
    }

    public void setKey2(int key2) {
        this.key2 = key2;
    }

    public OperationRand(int key1, int key2) {
        this.key1 = key1;
        this.key2 = key2;
    }

    @Override
    public String toString() {
        return "OperationRand{" +
                "key1=" + key1 +
                ", key2=" + key2 +
                '}';
    }
    }

谷歌的格森

    <dependency>

        <groupId>com.google.code.gson</groupId>

        <artifactId>gson</artifactId>

    </dependency>
[编辑]用于更改操作\u 我会将对象用于非更改字段


    import lombok.*;
    
    @Setter
    @Getter
    @AllArgsConstructor
    @NoArgsConstructor
    @ToString
    public class Key {
    int key1;
    int key2;
    }

大体上


    Gson gson = new Gson();
    Map<String,Key> rest = gson.fromJson(json,HashMap.class);
    System.out.println(rest);


Gson Gson=新的Gson();
Map rest=gson.fromJson(json,HashMap.class);
系统输出打印项次(剩余);
100不是地图

可能使用
Map
而不是
Map

通过
instanceof Integer
instanceof Map
来确定类型。

100不是Map

可能使用
Map
而不是
Map


通过
instanceof Integer
instanceof Map
来确定类型。

您只需将类简化为如下:

public class Operation {
    @JsonProperty("operation_rand")
    private Map operationDetails;
    
    public Map getOperation() {
        return operationDetails;
    }
} 

您只需简化类,如下所示:

public class Operation {
    @JsonProperty("operation_rand")
    private Map operationDetails;
    
    public Map getOperation() {
        return operationDetails;
    }
} 

有道理,但是我最初使用@JsonAnyGetter,因为我不知道
operation\u rand
字符串的值。上面的代码不是假设值总是
operation\u rand
?是的,但我认为我已经理解了要求。我将为非更改字段创建一个类,请在我放置示例代码的位置检查此代码共享。有道理,但是我最初使用@JsonAnyGetter,因为我不知道
operation\u rand
字符串的值。上面的代码不是假设值总是
operation\u rand
?是的,但我认为我已经理解了要求。我将为非更改字段创建一个类,请在我放置示例代码的位置检查此代码共享。