从 Tiled 使用 org.json.simple 生成的 JSON 文件加载数据

新手在这里...我需要单独解决此JSON文件中的以下两个“数据”数组,以便将它们保存在不同的int数组中:


//other stuff ...

"layers":[

{"data":[1, 1, 1, 1, 5, 1, 1, 1...],

//other stuff ...

}, 

{"data":[1, 1, 1, 1, 5, 1, 1, 1...],

//other stuff...

}

],

//other stuff...

}

这是我到目前为止的代码:


@SuppressWarnings("unchecked")

    private void loadJsonData() {

        JSONParser parser = new JSONParser();

        try {

            Object obj = parser.parse(new FileReader(path));

            String jsonStr = obj.toString();

            JSONObject json = (JSONObject) JSONValue.parse(jsonStr);


          //other code...


          mapTiles = ...;

          objectTiles = ...;


        } catch (ParseException ex) {

            ex.printStackTrace();

        } catch (FileNotFoundException ex) {

            ex.printStackTrace();

        } catch (IOException ex) {

            ex.printStackTrace();

        } catch (ClassCastException ex) {

            ex.printStackTrace();

        }

    }

我敢肯定答案是somwehere在那里,但我太无能了,找不到它:D


DIEA
浏览 113回答 1
1回答

冉冉说

有大量的资源,json组织库易于使用。您可以在此处找到更多示例JSONParser parser = new JSONParser();&nbsp; &nbsp; Object parsedObject = parser.parse(jsonStr);&nbsp; &nbsp; JSONObject jsonObject = (JSONObject) parsedObject;&nbsp; &nbsp; JSONArray layers = (JSONArray) jsonObject.get("layers");&nbsp;&nbsp; &nbsp; JSONObject data = (JSONObject) layers.get(0);&nbsp; &nbsp;&nbsp; &nbsp; JSONArray mapData = (JSONArray) data.get("data");&nbsp; &nbsp;&nbsp; &nbsp; int[] mapTile = new int[mapData.size()];&nbsp; &nbsp; for (int i = 0; i < mapData.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; mapTile[i] = ((Long)mapData.get(i)).intValue();&nbsp; &nbsp; }&nbsp; &nbsp; data = (JSONObject) layers.get(1);&nbsp;&nbsp;&nbsp; &nbsp; JSONArray objectData = (JSONArray) data.get("data");&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; int[] objectTile = new int[objectData.size()];&nbsp; &nbsp; for (int i = 0; i < objectData.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; objectTile[i] = ((Long)objectData.get(i)).intValue();&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(Arrays.toString(mapTile));&nbsp; &nbsp; System.out.println(Arrays.toString(objectTile));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java