GSON从JSON文件映射到Java类

时间:2017-02-23 04:00:33

标签: java json gson

我正在尝试将JSON文件映射到我用Java创建的一组类。关于我想要完成什么的小背景。我正在努力尝试“Pot - > Seed - > Harvest Basket - > Harvest Item”,其中: 锅:黄铜,粘土,木制 种子:谷物,水果 收获篮:(锅/种子将返回的项目列表) 收获内容:项目和数量范围

这是我设置的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分配值,所以从理论上讲,类设置应该是好的。

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
Response@6659c656
Exception in thread "main" java.lang.NullPointerException
at PlantsMain.JSONAssign(PlantsMain.java:56)
at PlantsMain.main(PlantsMain.java:36)

我尝试了其他的东西,比如传递给Get函数,但我想如果我不能返回类型类型,我就会遇到更大的问题。

1 个答案:

答案 0 :(得分: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);

希望它有所帮助。