jackson反序列化问题,将所有变量设置为null

时间:2017-12-04 11:12:10

标签: java json serialization jackson deserialization

我正在尝试,并且未能在杰克逊2.9.2中反序列化,我正在尝试制作一个游戏,对于一个单项目,我正在处理一个保存功能,我可以保存我的Player类,并序列化它,但当我尝试反序列化它,并从中创建一个新的播放器时,它将所有变量设置为null,这是我的方法:

public void LoadSaveString() throws IOException{
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    //module.addDeserializer(SaveFile.class, new SaveDeserializer());
    mapper.registerModule(module);

    String filePath = "files/SaveFile.json";
    BufferedReader reader = new BufferedReader(new FileReader(filePath)); 
    testfile = reader.readLine();

    System.out.println("Stringen testfilen bliver printet: " + testfile);
    Player player1 = (Player) mapper.readValue(testfile, Player.class);
    System.out.println("Test 2: " + player1.toString());

}

我得到的输出是:

  

Stringen testfilen bliver printet:{“player”:{“hp”:100,“air”:97,“inventory”:[],“hasCalledHelp”:false,“wonGame”:false,“playerName”:“ MADS”, “awesomePoints”:0 “totalTimePlayed”:3 “terminateThreads”:假 “stopThreadOxygen”:假 “stopThreadHP”:假, “姓名”: “MADS”}}

     

测试2:播放器{hp = 0,air = 0,inventory = null,hasCalledHelp = false,wonGame = false,awesomePoints = 0,totalTimePlayed = 0,terminateThreads = false,stopThreadOxygen = false,stopThreadHP = false}

我已经将我的播放器类设置为忽略未知属性,所以我只是,获取并设置对于播放器类重要的事情

@JsonIgnoreProperties(ignoreUnknown = true)

以及我的Player类中的属性:

public class Player {
private int hp;
private int air;
private ArrayList<Item> inventory;
private boolean hasCalledHelp = false;
private boolean wonGame = false;
public String playerName = "Mads"; // Non-negotiable
public int awesomePoints = 0;
public int totalTimePlayed = 0;

编辑:

文件SaveFile.json中的内容:

{
    "player": {
        "hp": 100,
        "air": 97,
        "inventory": [],
        "hasCalledHelp": false,
        "wonGame": false,
        "playerName": "Mads",
        "awesomePoints": 0,
        "totalTimePlayed": 3,
        "terminateThreads": false,
        "stopThreadOxygen": false,
        "stopThreadHP": false,
        "name": "Mads"
    }
}

2 个答案:

答案 0 :(得分:0)

您文件的JSON是Object,其属性为player。它不是Player

{
    "player": { "hp":100, ... }
}        

类似的东西:

public class PlayerWrapper {
    public player Player;
}

JSON应该是

{
    "hp":100,
    "air":97,
    "inventory":[],
    "hasCalledHelp":false,
    "wonGame":false,
    "playerName":"Mads",
    "awesomePoints":0,
    "totalTimePlayed":3,
    "terminateThreads":false,
    "stopThreadOxygen":false,
    "stopThreadHP":false,
    "name":"Mads"
}

或如果您无法更改文件,请使用PlayerWrapper

mapper.readValue(testfile, PlayerWrapper.class)

答案 1 :(得分:0)

首先,我建议您不要只读取第一行,而应将整个文件读取为字符串。只是为了确保支持具有其他格式的JSON文件。 此外,正如Tobias已经说过的那样,JSON文件不是一个“播放器”,它只包含一个播放器。除了Tobias的方法,您还可以在JSON文件中选择Player部分。

public void LoadSaveString() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    mapper.registerModule(module);

    String filePath = "files/SaveFile.json";

    //Read the whole file and not just one line
    byte[] encoded = Files.readAllBytes(Paths.get(filePath));
    String testfile = new String(encoded, "utf-8");

    JSONObject json = new JSONObject(testfile);
    //Pick only the player part
    String playerPart = json.getJSONObject("player").toString();

    System.out.println("Stringen testfilen bliver printet: " + testfile);
    Player player1 = (Player) mapper.readValue(playerPart, Player.class);
    System.out.println("Test 2: " + player1.toString());

}

Player类还需要每个属性的getter和setter:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Player {
    private int hp;
    private int air;
    private boolean hasCalledHelp = false;
    private boolean wonGame = false;
    public String playerName = "Mads"; // Non-negotiable
    public int awesomePoints = 0;
    public int totalTimePlayed = 0;

    public int getHp() {
        return hp;
    }
    public void setHp(int hp) {
        this.hp = hp;
    }
    public int getAir() {
        return air;
    }
    public void setAir(int air) {
        this.air = air;
    }
    public boolean isHasCalledHelp() {
        return hasCalledHelp;
    }
    public void setHasCalledHelp(boolean hasCalledHelp) {
        this.hasCalledHelp = hasCalledHelp;
    }
    public boolean isWonGame() {
        return wonGame;
    }
    public void setWonGame(boolean wonGame) {
        this.wonGame = wonGame;
    }
    public String getPlayerName() {
        return playerName;
    }
    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }
    public int getAwesomePoints() {
        return awesomePoints;
    }
    public void setAwesomePoints(int awesomePoints) {
        this.awesomePoints = awesomePoints;
    }
    public int getTotalTimePlayed() {
        return totalTimePlayed;
    }
    public void setTotalTimePlayed(int totalTimePlayed) {
        this.totalTimePlayed = totalTimePlayed;
    }
}