更改将对象图存储在JSON LIBGDX中



我在JSON文件的一般概念上遇到了一些问题,并写信给它们。目前,我有一个游戏项目的对象图,问题在于更改此地图。我有一个称为项目的类,如下所示,

  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文件,我可以将它们加载得很好。我想做的是更改我使用的项目的数量或其他任何方式,当前所有项目都加载到对象图中。我在我的保存((和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实际上,我认为做一个objoctmap.class的摘要是完全可以接受的,这就是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
}
}

我认为您有问题,因为您没有正确使用FromJson方法。Fromjson方法可以将JSON解析为您指定的类。因此,如果您指定项目。类,它将为您提供项目类的对象,然后您可以对该对象进行更改并保存回去。

这是一个小例子。

public void loadAndSaveJson() {
    FileHandle file = Gdx.files.external("test.json");
    Json json = new Json();
    Item itemToUpdate = json.fromJson(Item.class, file);
    itemToUpdate.setAmount(10000);
    json.setOutputType(JsonWriter.OutputType.json);
    file.writeString(json.prettyPrint(itemToUpdate), false);
    Item itemUpdated = json.fromJson(Item.class, file);
}

我对其进行了测试,并在JSON文件上更新了值。

还值得在这里阅读更多阅读&用libgdx

编写JSON

,所以实际上比我认为,问题是我如何更新对象映射的问题,我需要获得当前值,使其具有新的值,然后将其放回原处并删除旧。这在下面的示例中显示。

public void changeAmount(String key, int amount) {
    item temp = items.get(key);
    items.remove(key);
    temp.setAmount(amount);
    items.put(key, temp);
}

最新更新