简体   繁体   中英

Why does my method for parsing a JSON document only return lists with one element?

I'm working on a Java version of the board game RoboRally, and I'm currently working on a class that parses a JSON-document as a part of generating the game board. It almost works as intended, but not quite.

The JSON document is formated as a coordinate system containing information about what is on the board, and where it is located. The method generateJsonBoard() is supposed to parse the information from the document. It returns an ICell[][] array, where each Cell object on the array contains a List of pieces on top of the cell.

The method almost works, but it runs into issues when there is more than one element in the JSON array. It seems to only pick the last element of the array, and add that to the list, ignoring the others.

My question really boils down to if anyone sees why the List lists in my Cell objects only has one element even when the JSON array contains more than one (for instance in location[0][0].

Here is a part of the JSON-Document

 { "0": { "0": ["NorthWall","FlagOne","FlagTwo","FlagThree"], "1": ["NorthWall"], "2": ["NorthWall"], "3": ["NorthWall"], "4": ["NorthWall"], "5": ["NorthWall"], "6": ["NorthWall"], "7": ["NorthWall"], "8": ["NorthWall"], "9": ["NorthWall"] }, "1": { "0": ["WestWall"], "1": [], "2": [], "3": [], "4": [], ... ...

And here is the java code.

public class JSONBoardGenerator {

JSONParser parser = new JSONParser();

public ICell[][] generateJsonBoard(String filepath) {

    ICell[][] jsonBoardPieceList2 = null;

    try {
        Object boardFile = parser.parse(new FileReader(filepath));
        JSONObject jsonBoardFile = (JSONObject) boardFile;
        int jsonSize = jsonBoardFile.size();
        int jsonSide = jsonSize / jsonSize;
        jsonBoardPieceList2 = new ICell[10][10];
        System.out.println(jsonBoardFile);
        for (int x = 0; x <= 9; x++) {
            for (int y = 0; y <= 9; y++) {

                String intX = Integer.toString(x);
                String intY = Integer.toString(y);

                JSONObject xCord = (JSONObject) jsonBoardFile.get(intX);
                JSONArray yCord = (JSONArray) xCord.get(intY);
                Iterator<String> iterator = yCord.iterator();
                while (iterator.hasNext()) {
                    Cell tempCell = new Cell();
                    jsonBoardPieceList2[x][y] = tempCell;
                    String jsonIterator = iterator.next();
                    System.out.println(jsonIterator);
                    switch (jsonIterator) {
                        case "Hole":
                            System.out.println("Making Hole!");
                            break;

                        case "NorthWall":
                            System.out.println("Making northfacing wall!");
                            tempCell.addPiece(new Wall(Direction.NORTH));
                            break;

                        case "EastWall":
                            System.out.println("Making eastfacing wall!");
                            tempCell.addPiece(new Wall(Direction.EAST));
                            break;

                        case "SouthWall":
                            System.out.println("Making southfacing wall!");
                            tempCell.addPiece(new Wall(Direction.SOUTH));
                            break;

                        case "WestWall":
                            System.out.println("Making westfacing wall!");
                            tempCell.addPiece(new Wall(Direction.WEST));
                            break;

                        /**
                         case "ConveyorNorth":
                         System.out.println("Making northfacing conveyor!");
                         new Conveyor(Direction.NORTH, 1);
                         break;

                         case "ConveyorEast":
                         System.out.println("Making eastfacing conveyor!");
                         new Conveyor(Direction.EAST, 1);
                         break;

                         case "ConveyorSouth":
                         System.out.println("Making southfacing conveyor!");
                         new Conveyor(Direction.SOUTH, 1);
                         break;

                         case "ConveyorWest":
                         System.out.println("Making westfacing conveyor!");
                         new Conveyor(Direction.WEST, 1);
                         break;
                         */

                        case "FlagOne":
                            System.out.println("Making flag number 1");
                            tempCell.addPiece(new Flag(1));
                            break;

                        case "FlagTwo":
                            System.out.println("Making flag number 2");
                            tempCell.addPiece(new Flag(2));
                            break;

                        case "FlagThree":
                            System.out.println("Making flag number 3");
                            tempCell.addPiece(new Flag(3));
                            break;
                    }
                }
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return jsonBoardPieceList2;
}

}

Firgured it out. Worked fine using simple JSON. The only problem was that i placed Cell tempCell = new Cell(); too deep. It should've been inside the for-loop, not the while iterator. Thanks for the suggestions either way folks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM