Jackson解析器json setter将值作为字符串数组

时间:2013-06-04 17:41:39

标签: java json jackson gson

我在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\"}]";

我使用jackson解析器将其映射到一个类:

我希望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.

修改

如果杰克逊不支持,GSON可以支持此操作。

由于

3 个答案:

答案 0 :(得分: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]

答案 1 :(得分: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);

答案 2 :(得分:0)

您声明的字段为@JsonProperty String[] data

并使用

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

所以这个选项让杰克逊成为“foo”:“bar”和“foo”:[“a”,“b”,“c”]。

您需要做的就是将字符串数组合并到右侧行分隔符的字符串中。我是通过Guava data == null ? null : Jointer.on("\n").join(data)

来做的