使用键作为值反序列化Jackson

时间:2013-09-26 16:08:25

标签: java json jackson deserialization json-deserialization

我有一个类似于此的JSON结构:

"teams": {
    "team1Id": "team1Name",
    "team2Id": "team2Name"
}

我想将它反序列化为这些Java类:

class Teams {
Team team1;
Team team2;
}

class Team {
String id;
String name;
}

正如您所见,team1Id和team2Id(它们是JSON密钥)应该转换为Java字段的值。此外,第一个teamId / teamName对应该归因于存储在team1中的对象,而第二个对应存储在team2字段中。

是否有任何本机JACKSON映射器可以执行此操作,还是需要为此创建自己的自定义反序列化器?

1 个答案:

答案 0 :(得分:3)

您可以为此类实现自定义反序列化器,但我认为,更简单的解决方案是使用@JsonAnySetter注释。

class Teams {

    Team team1 = new Team();
    Team team2 = new Team();

    @JsonAnySetter
    public void anySetter(String key, String value) {
        if (key.startsWith("team1")) {
            team1.setId(key);
            team1.setName(value);
        } else if (key.startsWith("team2")) {
            team2.setId(key);
            team2.setName(value);
        }
    }

    //getters, setterr, toString()
}

使用示例:

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Wrapper wrapper = mapper.readValue(new File("X:/json"), Wrapper.class);
        System.out.println(wrapper.getTeams());
    }
}

class Wrapper {

    private Teams teams;

    public Teams getTeams() {
        return teams;
    }

    public void setTeams(Teams teams) {
        this.teams = teams;
    }
}

以上程序打印:

Teams [team1=Team [id=team1Id, name=team1Name], team2=Team [id=team2Id, name=team2Name]]

这个JSON:

{
    "teams": {
        "team1Id": "team1Name",
        "team2Id": "team2Name"
    }
}

###编辑1 ###

如果您的JSON看起来像这样:

{
    "teams": {
        "12345": "Chelsea",
        "67890": "Tottenham"
    }
}

我建议将其反序列化为LinkedHashMap<String, String>,然后将其转换为Teams对象。示例程序:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Wrapper wrapper = mapper.readValue(new File("X:/json"), Wrapper.class);
        System.out.println(wrapper.toTeams());
    }
}

class Wrapper {

    private LinkedHashMap<String, String> teams;

    public LinkedHashMap<String, String> getTeams() {
        return teams;
    }

    public void setTeams(LinkedHashMap<String, String> teams) {
        this.teams = teams;
    }

    public Teams toTeams() {
        List<Team> teamList = toTeamList();

        Teams result = new Teams();
        result.setTeam1(teamList.get(0));
        result.setTeam2(teamList.get(1));
        return result;
    }

    private List<Team> toTeamList() {
        List<Team> teamList = new ArrayList<Team>(teams.size());
        for (Entry<String, String> entry : teams.entrySet()) {
            Team team = new Team();
            team.setId(entry.getKey());
            team.setName(entry.getValue());
            teamList.add(team);
        }

        return teamList;
    }
}

以上程序打印:

Teams [team1=Team [id=12345, name=Chelsea], team2=Team [id=67890, name=Tottenham]]