Convert JSON with duplicated keys with jackson

戏子无情 提交于 2021-02-08 06:56:28

问题


Im converting a json to a java object but I'm not getting what I want. I have duplicated keys "friend" in my json.

My json:

{
    "id" : "5ee2e2f780bc8e7511a65de9",
    "friends": [{
        "friend": {
            "id": 1,
            "name": "Priscilla Lynch"
        },
        "friend": {
            "id": 2,
            "name": "William Lawrence"
        }
    }]
}

Using readValue from ObjectMapper only takes the last one "friend" but I need both. I know JSONObject use Map to convert so that's why it is taking the last one.

Result: Contacts(id=5ee2e2f780bc8e7511a65de9, friends=[{friend={id=2, name=William Lawrence}}])

ObjectMapper mapper = new ObjectMapper();
Contacts contacts = mapper.readValue(json, Contacts.class);

Contacts Pojo:

@Getter
@Setter
@ToString
public class Contacts {

    String id;
    List<Object> friends;
}

I want a list of all friends. As the service whose provide the json is not in my hand I need to find a way to solve it. I tried use MultiMap from apache.commons but no success. Im stuck on this.


回答1:


When you have a JSON Object with duplicated fields you can use com.fasterxml.jackson.annotation.JsonAnySetter annotation. Result will be a List<List<X>> so, you can use flatMap method to create List<X>. See below example:

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DuplicatedFieldsInJsonObjectApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = JsonMapper.builder().build();
        Contacts contacts = mapper.readValue(jsonFile, Contacts.class);
        System.out.println(contacts.getUnwrappedFriends());
    }
}

@Getter
@Setter
@ToString
class Contacts {

    String id;
    List<Friends> friends;

    public List<Friend> getUnwrappedFriends() {
        return friends.stream().flatMap(f -> f.getFriends().stream()).collect(Collectors.toList());
    }
}

class Friends {

    private List<Friend> friends = new ArrayList<>();

    @JsonAnySetter
    public void setAny(String property, Friend friend) {
        friends.add(friend);
    }

    public List<Friend> getFriends() {
        return friends;
    }
}

@Getter
@Setter
@ToString
class Friend {
    int id;
    String name;
}

Above code prints:

[Friend(id=1, name=Priscilla Lynch), Friend(id=2, name=William Lawrence)]



回答2:


Create POJO class of "Friend" also, then edit your "Contacts" class like

public class Contacts {
   String id;
   List<Friend> friends;
}

And then you should get the list of Friend items



来源:https://stackoverflow.com/questions/62353185/convert-json-with-duplicated-keys-with-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!