Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Gson - Fatal编程技术网

Java 解析JSON对象:解析后调用构造函数?

Java 解析JSON对象:解析后调用构造函数?,java,json,gson,Java,Json,Gson,对于JSON对象,我有以下POJO类: public class JSONChangeSet { public JSONChangeSet { System.out.println("Owner: " + owner); } @SerializedName("comment") private String comment; @SerializedName("lastUpdatedDate") private String mo

对于JSON对象,我有以下POJO类:

public class JSONChangeSet {

    public JSONChangeSet {
       System.out.println("Owner: " + owner);
    }

    @SerializedName("comment")
    private String comment;

    @SerializedName("lastUpdatedDate")
    private String modifiedDate;

    @SerializedName("owner")
    private Resource owner;

    @SerializedName("modifiedBy")
    private Resource modifier;

    public String getComment() {
        return comment;
    }

}

显然,这不起作用,因为在调用构造函数时,字段所有者尚未分配值。有没有可能在解析JSON对象后自动调用方法?

您用gson标记了您的问题,但我建议您使用,因为我看到了您的最后两个问题,并且gson似乎对您来说不够灵活

在Jackson中,您的示例如下所示:

public final class JSONChangeSet {
  private final String comment;
  private final Resource owner;

  @JsonCreator
  public JSONChangeSet(
    @JsonProperty("comment") final Resource owner,
    @JsonProperty("comment") final String comment
  ) {
    this.comment = comment;
    this.owner = owner;
  }

  public String getComment() {
    return comment;
  }
}
使用此解决方案,您可以拥有由构造函数构建的不可变对象。这对DI模式也有好处。顺便说一句,杰克逊是闪电般的快


您可能也想阅读。

您用gson标记了您的问题,但我建议您改为,因为我看到了您的最后两个问题,而且gson似乎对您来说不够灵活

在Jackson中,您的示例如下所示:

public final class JSONChangeSet {
  private final String comment;
  private final Resource owner;

  @JsonCreator
  public JSONChangeSet(
    @JsonProperty("comment") final Resource owner,
    @JsonProperty("comment") final String comment
  ) {
    this.comment = comment;
    this.owner = owner;
  }

  public String getComment() {
    return comment;
  }
}
使用此解决方案,您可以拥有由构造函数构建的不可变对象。这对DI模式也有好处。顺便说一句,杰克逊是闪电般的快


你可能还想阅读。

我认为Gson没有一个“听众”。您可以尝试以下技巧:

static class JSONChangeSet {

    @SerializedName("comment")
    private String comment;

    @SerializedName("owner")
    private int owner;

}

static class JSONChangeSetDeserializer implements JsonDeserializer<JSONChangeSet> {
    Gson gson = new Gson();

    @Override
    public JSONChangeSet deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {
        final JSONChangeSet obj = gson.fromJson(json, typeOfT);

        // Code you want to run
        System.out.println("Owner: " + obj.owner);

        return obj;
    }
}

public static void main(final String[] args) throws Exception, JsonMappingException, IOException {

    final GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(JSONChangeSet.class, new JSONChangeSetDeserializer());

    gson.create().fromJson("{\"comment\": \"it works!\", \"owner\": 23}", JSONChangeSet.class);

}
静态类JSONChangeSet{
@序列化名称(“注释”)
私有字符串注释;
@序列化名称(“所有者”)
私人业主;
}
静态类JSONChangeSetDeserializer实现JsonDeserializer{
Gson Gson=新的Gson();
@凌驾
公共JSONChangeSet反序列化(最终JsonElement json,最终类型typeOfT,
最终JsonDeserializationContext)抛出JsonParseException{
最终JSONChangeSet obj=gson.fromJson(json,typeOfT);
//要运行的代码
System.out.println(“所有者:“+obj.Owner”);
返回obj;
}
}
公共静态void main(最终字符串[]args)引发异常、JsonMappingException、IOException{
最终GsonBuilder gson=新的GsonBuilder();
registerTypeAdapter(JSONChangeSet.class,新的JSONChangeSetDeserializer());
gson.create().fromJson(“{\“comment\”:\“it works!\”,\“owner\”:23}”,JSONChangeSet.class);
}

我认为Gson没有“监听器”。您可以尝试以下技巧:

static class JSONChangeSet {

    @SerializedName("comment")
    private String comment;

    @SerializedName("owner")
    private int owner;

}

static class JSONChangeSetDeserializer implements JsonDeserializer<JSONChangeSet> {
    Gson gson = new Gson();

    @Override
    public JSONChangeSet deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {
        final JSONChangeSet obj = gson.fromJson(json, typeOfT);

        // Code you want to run
        System.out.println("Owner: " + obj.owner);

        return obj;
    }
}

public static void main(final String[] args) throws Exception, JsonMappingException, IOException {

    final GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(JSONChangeSet.class, new JSONChangeSetDeserializer());

    gson.create().fromJson("{\"comment\": \"it works!\", \"owner\": 23}", JSONChangeSet.class);

}
静态类JSONChangeSet{
@序列化名称(“注释”)
私有字符串注释;
@序列化名称(“所有者”)
私人业主;
}
静态类JSONChangeSetDeserializer实现JsonDeserializer{
Gson Gson=新的Gson();
@凌驾
公共JSONChangeSet反序列化(最终JsonElement json,最终类型typeOfT,
最终JsonDeserializationContext)抛出JsonParseException{
最终JSONChangeSet obj=gson.fromJson(json,typeOfT);
//要运行的代码
System.out.println(“所有者:“+obj.Owner”);
返回obj;
}
}
公共静态void main(最终字符串[]args)引发异常、JsonMappingException、IOException{
最终GsonBuilder gson=新的GsonBuilder();
registerTypeAdapter(JSONChangeSet.class,新的JSONChangeSetDeserializer());
gson.create().fromJson(“{\“comment\”:\“it works!\”,\“owner\”:23}”,JSONChangeSet.class);
}

只是一个设计/良好实践考虑事项:如果在构造函数中放置方法调用。确保这是最后一种方法。嗯,我只是稍微阅读了一下Jackson文档,但在我看来,如果某些字段是URI,那么Jackson无法通过JSON对象树进行解析,而这些URI也必须进行解析。@KARASZIIstván我知道。但是,因为这个问题意味着在反序列化过程之后调用一个方法,所以我理解您需要在构造函数中钩住调用来解决这个问题。或者我遗漏了什么:-)@Roflcoptr这不是真的。我认为Jackson可以解析可以写入文件的所有内容:)只是一个设计/良好实践考虑:如果在构造函数中放入方法调用。确保这是最后一种方法。嗯,我只是稍微阅读了一下Jackson文档,但在我看来,如果某些字段是URI,那么Jackson无法通过JSON对象树进行解析,而这些URI也必须进行解析。@KARASZIIstván我知道。但是,因为这个问题意味着在反序列化过程之后调用一个方法,所以我理解您需要在构造函数中钩住调用来解决这个问题。或者我遗漏了什么:-)@Roflcoptr这不是真的。我认为Jackson可以解析所有可以写入文件的内容:)