【发布时间】:2017-09-16 07:13:58
【问题描述】:
我对 JSON 文件的一般概念和写入它们有一些问题。目前,我有我的游戏项目的 ObjectMap,问题在于更改此地图。我有一个名为 item 的类,如下所示,
public static class Item {
public String name;
public String type;
public String rarity;
public int reqLevel;
int priceBuy;
int priceSell;
int amount = 0;
public item() {}
public void setAmount(int amnt) {amount = amnt;}
}
所以我的游戏中有一个包含所有这些项目的 json 文件,我可以完美地加载它们。我想要做的是使用 setAmount 或任何其他方式更改我拥有的项目的数量,目前所有项目都加载到 ObjectMap 中。我在我的 save() 和 load() 下面发布。我想我需要在写回地图之前更改地图中的项目,但我正在努力让它做任何事情。
public void save()
{
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
file.writeString(json.prettyPrint(items), false);
}
public void load() {
Json json = new Json();
items = json.fromJson(ObjectMap.class, file);
}
编辑:@Sneh 实际上我认为做一个 ObjectMap.class 的 fromJson 是完全可以接受的,这就是 JSON 文件的样子,这样你就可以拥有多个项目等。
*可能是错的,但它在我故意创建的场景中加载和编写完美,我遇到的问题是特定项目数量变量的更改!
//Test file with small amount of items
{
"SwordOfDeath": {
"class": "com.johnny.gamerpg.InventoryController$Sword",
"name": "Sword of Death",
"type" : "Sword",
"rarity": "Common",
"reqLevel": 1,
"proficiency": "Strength",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"DaggerOfDeath": {
"class": "com.johnny.gamerpg.InventoryController$Dagger",
"name": "Dagger of Death",
"type": "Dagger",
"rarity": "Rare",
"reqLevel": 3,
"proficiency": "Dexterity",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"DaggerOfLife": {
"class": "com.johnny.gamerpg.InventoryController$Dagger",
"name": "Dagger of Life",
"type": "Dagger",
"rarity": "Epic",
"reqLevel": 5,
"proficiency": "Dexterity",
"damageMax": 15,
"damageMin": 2,
"priceBuy": 1,
"priceSell": 2,
"bonusMagic": 0,
"bonusStrength": 0,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 0,
"amount": 2
},
"LifeSword": {
"class": "com.johnny.gamerpg.InventoryController$Sword",
"name": "Life Sword",
"type": "Sword",
"rarity": "Legendary",
"reqLevel": 45,
"proficiency": "Strength",
"damageMax": 100,
"damageMin": 99,
"priceBuy": 1000,
"priceSell": 2000,
"bonusMagic": 0,
"bonusStrength": 5,
"bonusDexterity": 0,
"bonusDefense": 0,
"bonusLuck": 10,
"amount": 2
}
}
【问题讨论】:
-
你能说明你在哪里创建和更改 items 对象吗?
标签: java android json libgdx hashmap