Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ObjectMap更改为存储在json Libgdx中_Java_Android_Json_Libgdx_Hashmap - Fatal编程技术网

Java 将ObjectMap更改为存储在json Libgdx中

Java 将ObjectMap更改为存储在json Libgdx中,java,android,json,libgdx,hashmap,Java,Android,Json,Libgdx,Hashmap,我对JSON文件的一般概念和编写有一些问题。目前,我有一个游戏物品的ObjectMap,问题在于更改此地图。我有一个名为item的类,如下所示 public static class Item { public String name; public String type; public String rarity; public int reqLevel; int priceBuy; int priceSell; in

我对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文件的样子,这样您就可以有多个项等等

*可能是错误的,但在我特意创建的场景中,它的加载和编写非常完美,我遇到的问题是更改特定的items amount变量

//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解析为您指定的类。所以,如果您指定Item.class,它将为您提供Item类的对象,然后您可以在该对象中进行更改并将其保存回去

这里有一个小例子

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文件中很好地更新了值


另外,这里值得更多阅读

我认为您遇到了问题,因为您没有正确使用fromJson方法。fromJson方法可以将json解析为您指定的类。所以,如果您指定Item.class,它将为您提供Item类的对象,然后您可以在该对象中进行更改并将其保存回去

这里有一个小例子

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文件中很好地更新了值


这里也值得更多阅读

,因此实际上比我想的更容易,问题在于我如何更新对象贴图,我需要获取当前值,给它一个新值,然后将其放回并删除旧值。下面的示例显示了这一点

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

所以这实际上比我想的更容易,问题是我如何更新对象映射,我需要得到当前值,给它一个新值,然后把它放回去,删除旧的。下面的示例显示了这一点

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

能否显示在何处创建和更改项目对象?能否显示在何处创建和更改项目对象?