2017-02-23 15 views
1

Javaで作成した一連のクラスにJSONファイルをマップしようとしています。私が成し遂げようとしていることについての少しの背景。私は "ポット - >シード - >ハーベストバスケット - >ハーベストアイテム"をしようとしています ポット:真ちゅう、クレイ、木製 種子:穀物、フルーツ ハーベストバスケット:種子は) 収穫の内容を返します。JSONファイルからJavaクラスへのマッピング

{ 
"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} 
      ] 
     } 
    ] 
    } 
] 
} 

] 
} 

私は以下のクラスを設定している:アイテムと数量の範囲

は、ここで私は、セットアップを(私はN00Bよ、優しくして)きたJSONです。私は手動でクラスに値を手動で追加しましたが、JSONで値を割り当てようとしていますので、理論的にはクラスの設定は良いはずです。

public class Pot { 
String potType; 
List <Seed> seedType; 

public Pot(String potType, List<Seed> seedItems) { 
    this.potType = potType; 
    this.seedType = seedItems; 
} 

public String getPotType() { 
    return this.potType; 
} 

public void setPotType(String potType) { 
    this.potType = potType; 
} 
} 

public class Seed { 
String seedType; 
List <HarvestBasket> harvestBasket; 

public Seed(String seedType, List <HarvestBasket> harvestBasket) { 
    this.seedType = seedType; 
    this.harvestBasket = harvestBasket; 
} 
} 

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

public HarvestBasket (int harvestBasketID, String harvestBasketName, int chance, List <HarvestContent> harvestContent) { 
    this.harvestBasketID = harvestBasketID; 
    this.harvestBasketName = harvestBasketName; 
    this.chance = chance; 
    this.harvestContent = harvestContent; 
} 

} 

public class HarvestContent { 
String harvestItem; 
int min, max; 

public HarvestContent(String harvestItem, int min, int max) { 
    this.harvestItem = harvestItem; 
    this.min = min; 
    this.max = max; 
} 
} 

public class HarvestContent { 
String harvestItem; 
int min, max; 

public HarvestContent(String harvestItem, int min, int max) { 
    this.harvestItem = harvestItem; 
    this.min = min; 
    this.max = max; 
} 
} 

だから、私は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によるnested json parsing"です。 – Haven

+0

ありがとうございます。私は何が間違っているのか分かります...クラス名と変数がJSONと一致しているかどうかを確認しませんでした。 –

関連する問題