杰克逊的问题问题将所有变量设置为空



我正在尝试,但在杰克逊(Jackson)2.9.2中未能进行挑选,我正在尝试制作游戏,用于一个我正在从事保存功能的Uni项目,我可以保存我的玩家类并序列化,但是当我尝试对其进行挑选并从中制作新播放器时,它将所有变量设置为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());
}

我得到的输出是:

串起testfilen bliver printet:{" player":{" hp":100," air":97,contectory':[]," hascalledHelp":false,false," wongame":wongame":false"," playername":"MADS"," AwesomePoints":0," totalTimePlayed":3," terminateThreads":false," propthreadoxygen":false," potthreadhp":false,false," name":" mads"}}}

测试2:player {hp = 0,air = 0,inting = null,hasCalledHelp = false,wongame = false,aveathypoints = 0,totaltimeplayed = 0,terminateThreads = false,propthreadoxygen = falseP>

我已经将玩家类设置为忽略未知属性,所以我只能获取并设置对Playerclass很重要的事物

@JsonIgnoreProperties(ignoreUnknown = true)

和我的玩家类中的属性:

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"
    }
}

首先,我建议您不仅读一行,而且还读了整个文件。只是为了确保支持其他格式的JSON文件。另外,正如Tobias已经说过的那样,JSON文件不是"播放器",它仅包含一个播放器。或者,与Tobias的方法相比,您也可以在JSON文件中选择播放器部分。

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());
}

还需要每个属性的玩家类别:

@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;
    }
}

文件的JSON是具有属性playerObject。这不是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)

最新更新