简体   繁体   English

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

[英]How can I map a duplicately named field when deserializing using Jackson?

I have a simple Java class that looks like this: 我有一个简单的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>>(){});
    }
}

I'm trying to deserialize the following bit of JSON: 我正在尝试反序列化以下JSON:

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

When i pass in the JSON into the constructor of the class AllAdverts , the JSON gets mapped properly except the field called advertId . 当我将JSON传递到AllAdverts类的构造函数中时,JSON会正确映射,除了名为advertId的字段advertId

As you can see, the JSON has two fields called id and _id , both of which I think Jackson treats as id and therefore, I am unable to load this. 如您所见,JSON具有两个名为id_id字段,我认为Jackson都将其视为id ,因此,我无法加载它。 How can I resolve this issue? 我该如何解决这个问题? I've been digging through the docs and thought that the JsonProperty annotation would solve this, but it hasn't worked. 我一直在研究文档,并认为JsonProperty批注可以解决这个问题,但是没有用。 While adgroupId has a value, advertId is null . 尽管adgroupId具有值,但advertIdnull

Is you mapper consuming/removing underscores and replacing the uppercase letters to lowercase ones to fill the bean properties? 映射器是否正在使用/删除下划线并将大写字母替换为小写字母以填充bean属性? Because that's what seems to be happening. 因为这似乎正在发生。 Jackson would not fill the adgroupId field if not. 否则,Jackson不会填写adgroupId字段。

Check your mapper. 检查您的映射器。

Here follows my code working with no problems at all (realize that I had to annotate the adgroupId field because of the name difference between the json and pojo field: 以下是我的代码完全没有问题的工作(意识到,由于json和pojo字段之间的名称不同,我必须注释adgroupId字段:

Main.java 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 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;

}

I'm using Jackson 2.4.1. 我正在使用Jackson 2.4.1。

I think that you have to provide custom names for all properties using JsonProperty annotation. 我认为您必须使用JsonProperty注释为所有属性提供自定义名称。 See example: 参见示例:

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

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

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

    // getters, setters

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM