5

我有以下json:

    "[{\"movieName\":\"A\",\"Leadactor\":\"\",\"leadActress\":\"\",\"movieTitle\":\"\",\"hero\":\"\",\"heroine\":\"\",\"source\":\"IMDB\"}," +
    "{\"movieName\":\"\",\"Leadactor\":\"\",\"leadActress\":\"\",\"movieTitle\":\"B\",\"hero\":\"B1\",\"heroine\":\"B2\",\"source\":\"Netflix\"}," +
    "{\"movieName\":\"C\",\"Leadactor\":\"C1\",\"leadActress\":\"C2\",\"movieTitle\":\"\",\"hero\":\"\",\"heroine\":\"\",\"source\":\"IMDB\"}," +
    "{\"movieName\":\"D\",\"Leadactor\":\"D1\",\"leadActress\":\"D2\",\"movieTitle\":\"\",\"hero\":\"\",\"heroine\":\"\",\"source\":\"IMDB\"}," +
    "{\"movieName\":\"\",\"Leadactor\":\"\",\"leadActress\":\"\",\"movieTitle\":\"E\",\"hero\":\"E1\",\"heroine\":\"E2\",\"source\":\"Netflix\"}]";

我正在使用杰克逊解析器将其映射到一个类:

我希望movieName 和movieTitle 映射到Java 类中的Name 属性。所以我写了下面的课程:

public static class MovieData {
    @JsonProperty("Name")
    private String name;

    @JsonSetter({"movieName"})
    private void setMovieName(final String name) {
            if((name != null) && (! name.equals(""))) {
                    setNameInternal(name);
            }
    }

    @JsonSetter("movieTitle")
    private void setMovieTitle(final String name) {
            if((name != null) && (! name.equals(""))) {
                    setNameInternal(name);
            }
    }

    private void setNameInternal(final String name) {
            this.name = name;
    }

}

在我的真实 json 中,有很多字段,例如 movieName、movieTitle,我想将它们规范化为一个通用名称。

有没有像下面这样的简单语法可以减少代码重复:

public static class MovieData {
    @JsonProperty("Name")
    private String name;

 @JsonSetter(value = { "movieName", "movieTitle" })
 private void setName(final String name) {
        if((name != null) && (! name.equals(""))) {
                this.name=name;
        }
}
  }

上面的代码在 jsonSetter 上给了我错误:

 Type mismatch: cannot convert from String[] to String.

编辑

如果Jackson 不支持,GSON 可以支持这个操作。

谢谢

4

3 回答 3

5

您可以使用@JsonAnySetter,这意味着您可以在Jackson Core (Data-Binding) Annotations页面上找到。

我创建了与您的示例相关的简单 bean:

class MovieData {

    private static List<String> NAME_PROPERTIES = Arrays.asList("movieName", "movieTitle");

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @JsonAnySetter
    private void parseUnknownProperties(String propertyName, String propertyValue) {
        if (NAME_PROPERTIES.contains(propertyName) && !propertyValue.isEmpty()) {
            this.name = propertyValue;
        }
    }

    @Override
    public String toString() {
        return name;
    }
}

现在,当我以这种方式反序列化您的 JSON 时:

ObjectMapper objectMapper = new ObjectMapper();
System.out.println(Arrays.toString(objectMapper.readValue(json, MovieData[].class)));

结果我可以看到:

[A, B, C, D, E]
于 2013-06-05T15:36:17.647 回答
3

不要做这么多。Gson 非常简单

为您的单组记录创建课程,例如

class Movie{
    private String movieName;
    private String Leadactor;
    private String leadActress;

    //put getter and setter for your fields
}

in main file
Type type = new TypeToken<List<Movie>>(){}.getType();
List<Movie> data = new Gson().fromJson(json_string,type);
于 2013-06-04T19:09:15.723 回答
0

您声明该字段@JsonProperty String[] data

并使用

 new ObjectMapper()
      .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
      .readValue(json, YOUT_TYPE.class)

所以这个选项使 Jackson 成为 "foo":"bar" 和 "foo": ["a", "b", "c"] 的一部分。

您需要做的就是将字符串数组合并回带有右行分隔符的字符串。我通过番石榴做data == null ? null : Jointer.on("\n").join(data)

于 2014-06-13T10:36:35.760 回答