2016-04-25 34 views
0

我不得不在Java中爲一個學校項目編寫一個小遊戲。 遊戲是RushHour,你可以在這裏看到一個例子:http://www.thinkfun.com/play-online/rush-hour/如何在Java中使用JSON文件的輸出來創建對象?

我的老師現在問我允許我的代碼讀取一個外部文件,以便遊戲的初始狀態不再被硬編碼在我的主體中,並且遊戲的參數可以自定義(大小板,在遊戲中的汽車數量...)

這是我的第一個JSON文件,它設置了一個經典的遊戲。

{ 
"ClassicBoard" : 
     { 
      "height" : "6", 
      "width" : "6", 
      "exit": 
       { 
        "row" : "2", 
        "column" : "5" 
       } 
     }, 
"ListOfCars" : 
     { 
      "car2" : 
       { 
        "char" : "2", 
        "size" : "3", 
        "orientation" : "vertical", 
        "currentPosition": 
          { 
          "row" : "2", 
          "column": "2" 
          } 
       },    
      "car3" : 
       { 
       "char" : "3", 
       "size" : "3", 
       "orientation" : "vertical", 
       "currentPosition": 
          { 
          "row" : "2", 
          "column": "4" 
          } 
       } 
     }, 
"redCar": 
     { 
     "char" : "1", 
     "size" : "2", 
     "orientation" : "horizontal", 
     "currentPosition": 
      { 
       "row" : "2", 
       "column": "0" 
      } 
     } 

}

我試圖找出如何讀取該文件,並重新使用它創建RushHourGame對象輸出。這是構造函數。

public RushHourGame(Board board, List <Car> cars, Car redCar) throws RushHourException 
{ 
    this.board = board; 
    this.redCar = redCar; 

    if(((redCar.getOrientation() == Orientation.HORIZONTAL) 
      && (board.getExit().getRow() != redCar.getCurrentPosition().getRow())) 
      || (((redCar.getOrientation() == Orientation.VERTICAL) 
      && (board.getExit().getColumn() != redCar.getCurrentPosition().getColumn())))) 
    { 
     throw new RushHourException("Car must be aligned with the exit, " 
       + "a default game has been created"); 
    } 
    board.put(redCar); 
    for (Car putCars : cars) 
    { 
     board.put(putCars); 
    } 
} 

我試着用這樣的BufferedReader。

public static String readFile(String filename) 
{ 
    String result =""; 
    try 
    { 
     BufferedReader br = new BufferedReader (new FileReader(filename)); 
     StringBuilder sb = new StringBuilder(); 
     String line = br.readLine(); 
     while (line != null) 
     { 
      sb.append(line); 
      line = br.readLine(); 
     } 
     result = sb.toString(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return result; 
} 

但我不是如何使用它來解析我的JSON文件。有人可以幫助我嗎?

回答

0

您的問題的多個答案可以找到here。我更喜歡通過StaxMan提供了答案:

最簡單的方法是Jackson

MyObject ob = new ObjectMapper().readValue(jsonString, RushHourState.class); 

上面的代碼會自動處理JSON/Java對象綁定,使用Java的POJO提供如下:

public class RushHourState { 
    private ArrayList<Car> listOfCars; 
    private ClassicBoard classicBoard; 
    private Car redCar; 

    public ArrayList<Car> getListOfCars() { 
     return listOfCars; 
    } 
    public void setListOfCars(ArrayList<Car> listOfCars) { 
     this.listOfCars = listOfCars; 
    } 
    public ClassicBoard getClassicBoard() { 
     return classicBoard; 
    } 
    public void setClassicBoard(ClassicBoard classicBoard) { 
     this.classicBoard = classicBoard; 
    } 
    public Car getRedCar() { 
     return redCar; 
    } 
    public void setRedCar(Car redCar) { 
     this.redCar = redCar; 
    } 
} 

// --------------------------------------------------- 

public class ClassicBoard { 
    private int height; 
    private int width; 
    private HashMap<String, String> exit; 
    public int getHeight() { 
    return height; 
    } 
    public void setHeight(int height) { 
    this.height = height; 
    } 
    public int getWidth() { 
    return width; 
    } 
    public void setWidth(int width) { 
    this.width = width; 
    } 
    public HashMap<String, String> getExit() { 
    return exit; 
    } 
    public void setExit(HashMap<String, String> exit) { 
    this.exit = exit; 
    } 
} 

// --------------------------------------------------- 

public class Car { 
    private int charr; 
    private int size; 
    private String orientation; 
    private HashMap<String, String> currentPosition; 
    public int getCharr() { 
     return charr; 
    } 
    public void setCharr(int charr) { 
     this.charr = charr; 
    } 
    public int getSize() { 
     return size; 
    } 
    public void setSize(int size) { 
     this.size = size; 
    } 
    public String getOrientation() { 
     return orientation; 
    } 
    public void setOrientation(String orientation) { 
     this.orientation = orientation; 
    } 
    public HashMap<String, String> getCurrentPosition() { 
     return currentPosition; 
    } 
    public void setCurrentPosition(HashMap<String, String> currentPosition) { 
     this.currentPosition = currentPosition; 
    } 
} 

注意:我將Car類中的變量'char'重命名爲'charr',因爲char是Java保留關鍵字。希望你覺得這有幫助!

+0

感謝您的回答。你的回答看起來很有意思,我會盡快詳細檢查,如果有效,我會回來給你打電話! –

+0

嗯,我認爲這段代碼不是很有用,因爲我已經爲Car,Board,Position ...做了我的課程,而你所做的不適合它。另外,我認爲使用setter不是解決方案,因爲我不希望用戶有可能更改這些值。 RushHourGame類的構造函數調用所有其他類的cnstructor,因此,我只需要RushHourGame的構造函數來檢索JSON文件中的數據和值,並使用它創建一個新的RushHourGame對象。 但謝謝傑克遜的解決方案。 –