1

我正在尝试使用 genson 将 json 字符串反序列化为 java 对象,但未能成功。我的班级结构是这样的:

public class Condition {
}

public class SimpleCondition extends Condition {
    String feature;
    String op;
    String value;
    int primitive;
}

public class ComplexCondition extends Condition {
    private Condition left;
    private String joint;
    private Condition right;
}

如您所见, andComplexCondition可能有 anotherComplexCondition或 aSimpleCondition作为其成员。我得到的json是这样的:leftright

{
  "left": {
            "feature":"locality",
            "op":"==",
            "value":"Chino"
            "primitive":9,
          },
  "joint":"and",
  "right": {
              "feature":"ch4",
              "op":">=",
              "value":"1.5",
              "primitive":3
           }
}

在这个 json 中,a既有ComplexConditions 又有lefts 。但是我收到的一般 json 字符串可以是从 a到 s 的任何嵌套级别的任何内容。我尝试在 json 字符串中设置值,但 genson 仍然无法反序列化它。我感谢使用任何库将此 json 反序列化为 java 的任何帮助。rightSimpleConditionSimpleConditionComplexCondition@class

4

2 回答 2

1

您可以为您的类注册一些别名,然后在您的 json 中引用它们,如下所示:

Genson genson = new GensonBuilder()
    .addAlias("ComplexCondition", ComplexCondition.class)
    .addAlias("SimpleCondition", SimpleCondition.class)
    .create();

{
  "@class": "ComplexCondition",
  "left": {
            "@class": "SimpleCondition",
            "feature":"locality",
            "op":"==",
            "value":"Chino",
            "primitive":9
          },
  "joint":"and",
  "right": {
            "@class": "SimpleCondition",
              "feature":"ch4",
              "op":">=",
              "value":"1.5",
              "primitive":3
           }
}

您还需要为您的 ComplexCondition 添加 get 和 set 方法,或者将其字段公开,或者提供一个将它们作为参数的构造函数,或者配置 genson 以使用私有字段。

最后要注意的是,类元数据属性必须在没有@前缀的属性之前定义。如果您使用 Genson 生成此 json,它将始终遵守此约束。

于 2016-05-25T01:42:06.123 回答
0

我想出了这个解决方案。如果有人能给出更好的解决方案,我很乐意接受。

import org.json.JSONObject;
public class Condition {
    public static Condition getCondition(JSONObject json){
        if(json.has("left"))
            return new ComplexCondition(json);
        else if(json.has("feature"))
            return new SimpleCondition(json);
        else
            throw new IllegalArgumentException("Invalid json for a Condition");
    }
}

public class ComplexCondition extends Condition{
    private Condition left;
    private String joint;
    private Condition right;

    public ComplexCondition(JSONObject json){
        if(json.has("left")){
            JSONObject leftJSON = json.getJSONObject("left");
            if(leftJSON.has("left"))
                this.left = new ComplexCondition(leftJSON);
            else
                this.left = new SimpleCondition(leftJSON);

            this.joint = json.getString("joint");

            JSONObject rightJSON = json.getJSONObject("right");
            if(rightJSON.has("left"))
                this.right = new ComplexCondition(rightJSON);
            else
                this.right = new SimpleCondition(rightJSON);
        } else {
            throw new IllegalArgumentException("invalid json for a complex condition");
        }
    }

    //getters and setters
}

public class SimpleCondition extends Condition{
    private String feature;
    private String op;
    private String value;
    private int primitive;

    public SimpleCondition(JSONObject json) {
        if(json.has("feature")){
            this.feature = json.getString("feature");
            this.op = json.getString("op");
            this.value = json.getString("value");
            this.primitive = json.getInt("primitive");
        } else
            throw new IllegalArgumentException("invalid json for a simple condition");

    }

    //getters and setters
}
于 2016-05-24T23:21:43.820 回答