2017-12-04 2 views
0

私はジャーソン2.9.2でデシリアライズできませんでした。私は、ユニ・プロジェクトのためにゲームを作ろうとしています。私は私のプレイヤークラスを保存し、それをシリアライズが、私はそれをデシリアライズし、それから、新しいプレーヤーを作成しようとすると、それはnullにすべての変数を設定し、これが私の方法であることができる場所:ジャーソンデシリアライズの問題は、すべての変数を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:{ "プレーヤー":{ "馬力":100、 "空気":97、 "在庫":[]、 "hasCalledHelp":偽、 "wonGame" :false、 "playe 0 "、" totalTimePlayed ":3、" terminThreads ":false、" stopThreadOxygen ":false、" stopThreadHP ":false、" name ":" Mads "}}

試験2:プレーヤー{HP = 0、空気= 0、在庫=ヌル、hasCalledHelp =偽、wonGame =偽、awesomePoints = 0、totalTimePlayed = 0、terminateThreads =偽、stopThreadOxygen =偽、stopThreadHP =偽}

と私は、未知の特性を無視し、私のプレーヤーのクラスを設定しているので、私は唯一、取得と設定、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" 
    } 
} 
+0

あなたはSaveFile.jsonのコンテンツを投稿できますか? – JanTheGun

+0

投稿を編集しました –

答えて

0
それは Player

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

何かのようではありません

まず最初に、最初の行だけでなくファイル全体を文字列に読み込むことをお勧めします。別の形式のJSONファイルがサポートされていることを確認するだけです。 また、Tobiasが既に述べたように、JSONファイルは単にPlayerを含む「Player」ではありません。 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()); 

} 

またプレイヤークラスは、属性ごとに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; 
    } 
} 
+0

ありがとうございます –

0

ファイルのJSON属性playerを持っObjectです。

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) 
関連する問題