2017-02-23 39 views
1

我想將JSON文件映射到我在Java中創建的一組類。我試圖完成的小背景。我想要一個「鍋 - >種子 - >收穫籃子 - >收穫項目」,其中: 鍋:黃銅,粘土,木 種子:穀物,水果 收穫籃子:(一鍋/種子將返回) 收穫內容:該項目和數量範圍從JSON文件到Java類的GSON映射

這裏的JSON我設置(溫柔,我的n00b):

{ 
"Pot": [ 
{"potType": "Clay", 
"Seed": [ 
    {"seedType": "Grain", 
    "HarvestBasket" : [ 
     {"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Wheat", "min": 3, "max": 6}, 
      {"harvestItem": "Corn", "min": 1, "max": 3} 
      ] 
     }, 
     {"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50, 
     "harvestContent": [ 
      {"harvestItem": "Rice", "min": 10, "max": 12} 
      ] 
     }, 
     {"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Corn", "min": 1, "max": 3}, 
      {"harvestItem": "Spelt", "min": 5, "max": 6} 
      ] 
     } 
    ] 
    }, 
    {"seedType": "Fruit", 
    "HarvestBasket" : [ 
     {"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Apple", "min": 3, "max": 5}, 
      {"harvestItem": "Orange", "min": 1, "max": 3} 
      ] 
     }, 
     {"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50, 
     "harvestContent": [ 
      {"harvestItem": "Tangerine", "min": 10, "max": 12} 
      ] 
     }, 
     {"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Kiwi", "min": 1, "max": 3}, 
      {"harvestItem": "Apple", "min": 5, "max": 6} 
      ] 
     } 
    ] 
    } 
] 
}, 

{"potType": "Brass",  
"Seed": [ 
    {"seedType": "Grain", 
    "HarvestBasket" : [ 
     {"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Wheat", "min": 20, "max": 20}, 
      {"harvestItem": "Corn", "min": 20, "max": 20} 
      ] 
     }, 
     {"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50, 
     "harvestContent": [ 
      {"harvestItem": "Rice", "min": 20, "max": 20} 
      ] 
     }, 
     {"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Corn", "min": 1, "max": 3}, 
      {"harvestItem": "Spelt", "min": 20, "max": 20} 
      ] 
     } 
    ] 
    }, 
    {"seedType": "Fruit", 
    "HarvestBasket" : [ 
     {"harvestBasketID": 1, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Apple", "min": 25, "max": 25}, 
      {"harvestItem": "Orange", "min": 25, "max": 25} 
      ] 
     }, 
     {"harvestBasketID": 2, "harvestBasketName": "temp", "chance": 50, 
     "harvestContent": [ 
      {"harvestItem": "Tangerine", "min": 25, "max": 25} 
      ] 
     }, 
     {"harvestBasketID": 3, "harvestBasketName": "temp", "chance": 25, 
     "harvestContent": [ 
      {"harvestItem": "Kiwi", "min": 25, "max": 25}, 
      {"harvestItem": "Apple", "min": 25, "max": 25} 
      ] 
     } 
    ] 
    } 
] 
} 

] 
} 

我有以下類別的設置。請注意,我已經手動爲這些類添加了值,並且現在正試圖讓JSON分配這些值,所以從理論上講,類設置應該很好。所以,我使用的是GSON,它似乎在讀取文件,但是當我輸出任何東西時,我得到了nullPointer異常。我不知道如果導入不工作或者是什麼......下面是我搞亂代碼:

class Response { 
Map<String, Pot> myPot; 

public Map<String, Pot> getPot() { 
    return myPot; 
} 
} 

    public static void JSONAssign() { 

    Gson gson = new Gson(); 
    BufferedReader br = null; 


    try { 
     br = new BufferedReader(new FileReader("/Users/patkhumprakob/Documents/workspace/Plants/src/harvestLoot.json")); 

     Response response; 
     try { 
      response = gson.fromJson(br, Response.class); 
      System.out.println(response.hashCode()); 
      System.out.println(response.toString()); 

      System.out.println(response.getPot().getClass()); 

控制檯響應我得到的是:

1717159510 
[email protected] 
Exception in thread "main" java.lang.NullPointerException 
at PlantsMain.JSONAssign(PlantsMain.java:56) 
at PlantsMain.main(PlantsMain.java:36) 

我試過其他比如傳遞給Get函數,但是我想如果我不能返回類的類型,我會遇到更大的問題。

回答

1

定義這些類:

class Pots { 
    List<Pot> Pot; 
    } 

    class Pot { 
    protected String potType; 
    protected List<Seed> Seed; 
    } 

    class HarvestBasket { 
    protected String harvestBasketID; 
    protected String harvestBasketName; 
    protected int chance; 
    protected List<HarvestContent> harvestContent; 
    } 

    class HarvestContent { 
    protected String harvestItem; 
    protected int min; 
    protected int max; 
    } 

    class Seed { 
    protected String seedType; 
    protected List<HarvestBasket> HarvestBasket; 
    } 

然後使用:

Gson gson = new Gson(); 
Pots pots = gson.fromJson("Your JSON content", Pots.class); 

希望它能幫助。

+0

謝謝。將在早上嘗試。你能解釋爲什麼你的解決方案有效嗎?我試圖學習......它是否將類名映射到我搞砸了的JSON? –

+0

@PatK:請參考這個http://stackoverflow.com/questions/19169754/parsing-nested-json-data-using-gson 關鍵字是「由gson嵌套的json解析」。 – Haven

+0

謝謝。我看到我做錯了......沒有確定類名和變量與JSON匹配。 –