使用键作为值反序列化Jackson [英] Deserializing Jackson by using a key as value

查看:137
本文介绍了使用键作为值反序列化Jackson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSON structure similar to this:

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

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

and I would like to deserialize it to these Java classes:

class Teams {
Team team1;
Team team2;
}

class Team {
String id;
String name;
}

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

As you can see team1Id and team2Id, which are JSON keys, should be converted into values of Java fields. Additionally, the first teamId/teamName pair should be attributed to the object stored in team1, while the second pair is stored in the team2 field.

是否有任何本机JACKSON映射器要做到这一点,还是我需要为此创建自己的自定义反序列化器?

Are there any native JACKSON mappers to do this, or will I need to create my own custom deserializer for this?

推荐答案

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

You can implement custom deserializer for this class, but, I think, much simpler solution will be using @JsonAnySetter annotation.

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的$​​ b
$ b

for this JSON:

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

###编辑1 ###

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

If your JSON looks like that:

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

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

I propose to deserialize it to LinkedHashMap<String, String> and after that convert it to Teams object. Example program:

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]]

这篇关于使用键作为值反序列化Jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆