在使用Jackson进行反序列化时,如何映射重复命名的字段?

时间:2014-08-14 12:22:27

标签: java json jackson deserialization json-deserialization

我有一个简单的Java类,如下所示:

public class AllAdverts {

    @JsonIgnoreProperties(value="id", ignoreUnknown=true)
    public static class Advert {

        @JsonProperty(value="_id")
        public String advertId;

        public String adgroupId;
    }

    public List<Advert> adverts;

    public AllAdverts(String json) throws IOException {
        this.adverts = MyUtils.mapper.readValue(json, 
                new TypeReference<List<Advert>>(){});
    }
}

我正在尝试反序列化以下JSON:

[
    {
        "_id": "535788abf789b8916a8b456e",
        "adgroup_id": "535788abf789b8916a8b456c",
        "subcampaign_id": "535788abf789b8916a8b4569",
        "id": 6005879321807,
    },
    {
        "_id": "535788abf789b8916a8b456f",
        "adgroup_id": "535788abf789b8916a8b456c",
        "subcampaign_id": "535788abf789b8916a8b4569",
        "id": 6005879319007,
    }
]

当我将JSON传递给类AllAdverts的构造函数时,JSON会被正确映射,但名为advertId的字段除外。

正如您所看到的,JSON有两个名为id_id的字段,我认为杰克逊都将其视为id,因此我无法加载此字段。我该如何解决这个问题?我一直在挖掘文档,并认为JsonProperty注释可以解决这个问题,但它没有奏效。虽然adgroupId有值,但advertIdnull

2 个答案:

答案 0 :(得分:1)

映射器是否正在使用/删除下划线并将大写字母替换为小写字母以填充bean属性?因为这似乎正在发生。如果没有,杰克逊就不会填补adgroupId字段。

检查你的映射器。

下面我的代码完全没有问题(因为json和pojo字段之间的名称差异,我必须注释adgroupId字段:

<强> Main.java

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

    public static void main(String[] args) throws Exception {
        new Main().start();
    }

    public List<Advert> adverts;

    private void start() throws Exception {

        String jsonString =
                "[" +
                    "{" +
                        "\"_id\": \"535788abf789b8916a8b456e\"," +
                        "\"adgroup_id\": \"535788abf789b8916a8b456c\"," +
                        "\"subcampaign_id\": \"535788abf789b8916a8b4569\"," +
                        "\"id\": 6005879321807," +
                    "}," +
                    "{" +
                        "\"_id\": \"535788abf789b8916a8b456f\"," +
                        "\"adgroup_id\": \"535788abf789b8916a8b456c\"," +
                        "\"subcampaign_id\": \"535788abf789b8916a8b4569\"," +
                        "\"id\": 6005879319007," +
                    "}" +
                "]";

        ObjectMapper mapper = new ObjectMapper();

        JSONArray jsonArray = new JSONArray(jsonString);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Advert advert = mapper.readValue(jsonObject.toString(), Advert.class);

            System.out.println("advertId: " + advert.advertId);
            System.out.println("adgroupId: " + advert.adgroupId);
        }

    }

}

<强> Advert.java

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

@JsonIgnoreProperties(value = "id", ignoreUnknown = true)
public class Advert {

    public Advert() {}

    @JsonProperty(value = "_id")
    public String advertId;

    @JsonProperty(value = "adgroup_id")
    public String adgroupId;

}

我正在使用Jackson 2.4.1。

答案 1 :(得分:0)

我认为您必须使用JsonProperty注释为所有属性提供自定义名称。见例:

@JsonIgnoreProperties(value = "id", ignoreUnknown = true)
class Advert {

    @JsonProperty(value = "_id")
    public String advertId;

    @JsonProperty(value = "adgroup_id")
    public String adgroupId;

    // getters, setters

}