首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中使用JSON文件的输出来创建对象?

如何在Java中使用JSON文件的输出来创建对象?
EN

Stack Overflow用户
提问于 2016-04-26 00:54:24
回答 1查看 156关注 0票数 0

我不得不为学校的一个项目用Java编写了一个小游戏。这个游戏是RushHour,你可以在这里看到一个例子:http://www.thinkfun.com/play-online/rush-hour/

我的老师现在要求我允许我的代码读取外部文件,这样游戏的初始状态就不再是硬编码在我的main中,并且游戏的参数可以自定义(棋盘的大小,游戏中的汽车数量……)

这是我的第一个JSON文件,它设置了一个经典游戏。

代码语言:javascript
复制
{
"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对象。这是构造函数。

代码语言:javascript
复制
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,就像这样。

代码语言:javascript
复制
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文件。有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2016-04-26 01:31:36

你的问题的多个答案可以在here上找到。我更喜欢StaxMan提供的答案:

最简单的选项是Jackson

代码语言:javascript
复制
MyObject ob = new ObjectMapper().readValue(jsonString, RushHourState.class);

上面的代码应该会使用下面提供的Java POJO自动处理JSON/Java对象绑定:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36846592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档