[
{
"locations": {
"description": "You look around the room and see you are in an empty room with 2 doors to the left and to the right. Knowing not how you got there, you decide to figure out how to escape and get back to your normal life.",
"name": "start",
"objects": [ "" ],
"directions": [{"right":"empty room1"}, {"left":"dungeon"}]
}
},
{
"locations": {
"description": "Inside this room it looks like some sort of dungeon with a cage in the middle and blood lining the wall and floor.",
"name": "dungeon",
"objects": [ "map", "torch" ],
"directions": [{"up":"hallway2"}, {"down":"hallway1"}, {"right":"start"}]
}
}
]
上面是我正在使用Java开发的基于文本的游戏的JSON文件的摘要。我能够解析出一个不同的文件,该文件在每个位置(对象和方向)都没有另一个数组,但是这个文件确实有一个数组,我非常困惑。我需要将对象添加到字符串数组中,并将方向放入地图中。下面是我用来解析它并将其添加到类对象数组的代码!我知道我试图将它们添加到字符串数组中的做法是完全错误的,但是我离开了它,因此希望它能更好地理解我的尝试。预先感谢您的帮助!
JSONParser jsonParser2 = new JSONParser();
try (FileReader reader = new FileReader("Locations.json")) {
//Read JSON file
Object obj = jsonParser2.parse(reader);
JSONArray locationsList = (JSONArray) obj;
System.out.println(locationsList);
//Iterate over locations array
locationsList.forEach(emp -> parseJSONLocations((JSONObject) emp, locations));
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException | ParseException e) {
System.out.println(e);
}
private static void parseJSONLocations(JSONObject locations, Locations[] location) {
//Get locations object within list
JSONObject locationObject = (JSONObject) locations.get("locations");
//Get location description
String desc = (String) locationObject.get("description");
System.out.println(desc);
//Get location name
String name = (String) locationObject.get("name");
System.out.println(name);
//Get location objects
String[] objects = (String[]) locationObject.get("objects");
for (String o : objects) {
System.out.println(o);
}
//get location directions (direction : location)
Map<String, String> directions = (Map<String, String>) locationObject.get("directions");
for (String key : directions.keySet()) {
String value = directions.get(key);
System.out.println(key + " " + value);
}
//location[index] = new Locations(desc, name, objects, directions);
index++;
}
因此,我最终将其与这些更改一起使用!在此项目之前,我从未使用过JSON,而且显然我将其写错了。
JSONParser jsonParser2 = new JSONParser();
try (FileReader reader = new FileReader("Locations.json")) {
Object obj = jsonParser2.parse(reader);
JSONArray locationsList = (JSONArray) obj;
//Iterate over locations array
locationsList.forEach(emp -> parseJSONLocations((JSONObject) emp, locations, objects));
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException | ParseException e) {
System.out.println(e);
}
然后将所有内容添加到类的方法更改为:
private static void parseJSONLocations(JSONObject locations, Locations[] location, Objects[] object) {
JSONObject locationObject = (JSONObject) locations.get("locations");
String desc = (String) locationObject.get("description");
String name = (String) locationObject.get("name");
JSONArray objects = (JSONArray) locationObject.get("objects");
Iterator<String> it = objects.iterator();
List<Objects> objs = new ArrayList<>();
while (it.hasNext()) {
String mStr = it.next();
for (Objects elm : object) {
if (elm.getName().equals(mStr)) {
objs.add(elm);
}
}
}
JSONArray directions = (JSONArray) locationObject.get("directions");
Map<String, String> map = new HashMap<>();
Iterator<JSONObject> it2 = directions.iterator();
while (it2.hasNext()) {
JSONObject value = it2.next();
map.put((String) value.get("direction"), (String) value.get("location"));
}
location[index2] = new Locations(desc, name, objs, map);
index2++;
}
但随后,根据jgolebiewski的建议,我还必须更改JSON代码:
[
{
"locations": {
"description": "You look around the room and see you are in an empty room with 2 doors to the left and to the right. Knowing not how you got there, you decide to figure out how to escape and get back to your normal life.",
"name": "start",
"objects": [],
"directions": [{
"direction": "right",
"location": "empty room1"
}, {
"direction": "left",
"location": "dungeon"
}]
}
},
{
"locations": {
"description": "Inside this room it looks like some sort of dungeon with a cage in the middle and blood lining the wall and floor.",
"name": "dungeon",
"objects": ["map", "torch"],
"directions": [{
"direction": "up",
"location": "hallway2"
}, {
"direction": "down",
"location": "hallway1"
}, {
"direction": "right",
"location": "start"
}]
}
}
]
也感谢您对本教程的帮助:https : //howtodoinjava.com/library/json-simple-read-write-json-examples/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句