2017-04-19 3 views
1

JSON 파일의 일반적인 개념과 쓰기에 몇 가지 문제가 있습니다. 현재, 게임 아이템의 ObjectMap을 가지고 있는데, 문제는이 맵을 변경하는 것입니다. 아래에 표시된 항목이라는 클래스가 있습니다.ObjectMap을 json Libgdx에 저장하도록 변경

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 
} 
} 
+0

항목 개체를 만들고 변경하는 위치를 표시 할 수 있습니까? – Sneh

답변

0
그래서

실제로 내가, 문제는 내가 객체 맵을 업데이트하는 상황이라고 생각 다음 쉬웠다, 나는 현재 값이 그것에게 새로운 가치를 부여받을 필요하고 더 여기 읽을 가치 그것을 다시 넣고 낡은 것을 지우십시오. 이것은 아래 예에 나와 있습니다.

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

나는 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 파일에서 값을 잘 갱신했다.

또한 Reading & Writing JSON with LibGdx

+0

편집 ** @ Sneh ** 읽기 – johnnyboy5566