gson 将JSON字符串转换为java类

h9vpoimq  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(157)

我有一个JSON字符串,我想把它转换成Java类,但是当我用Gson把它转换成Java类并在控制台中打印时,它显示为null。
这是我的JSON字符串:

{
    "first": [
        {
            "price_1": {
                "material_name": "EMERALD",
                "amount": 5
            },
            "price_2": {
                "material_name": "STONE",
                "amount": 5
            },
            "out": {
                "material_name": "EMERALD_BLOCK",
                "amount": 1
            }
        },
        {
            "price_1": {
                "material_name": "EMERALD",
                "amount": 7
            },
            "out": {
                "material_name": "DIAMOND_AXE",
                "amount": 0
            }
        }
    ]
}

字符串
这些是我的课程

public class TradeListController {
    public static List<MerchantRecipe> get_trade_list(String trade_list_name) {
        String jsonString = "..."
        Gson gson = new GsonBuilder().create();
        Type type = new TypeToken<TradeList>() {}.getType();
        TradeList tradeList = gson.fromJson(jsonString, type);
        System.out.println(String.valueOf(tradeList));
        return null;
    }
}

class TradeList {
    private Map<String, List<TradeItem>> dynamicItems;

    public Map<String, List<TradeItem>> getDynamicItems() {
        return dynamicItems;
    }

    public void setDynamicItems(Map<String, List<TradeItem>> dynamicItems) {
        this.dynamicItems = dynamicItems;
    }

    @Override
    public String toString() {
        return "TradeList{" +
                "dynamicItems=" + dynamicItems +
                '}';
    }
}

class TradeItem {
    private Price price_1;
    private Price price_2;
    private Price out;

    public Price getPrice_1() {
        return price_1;
    }

    public void setPrice_1(Price price_1) {
        this.price_1 = price_1;
    }

    public Price getPrice_2() {
        return price_2;
    }

    public void setPrice_2(Price price_2) {
        this.price_2 = price_2;
    }

    public Price getOut() {
        return out;
    }

    public void setOut(Price out) {
        this.out = out;
    }

    @Override
    public String toString() {
        return "TradeItem{" +
                "price_1=" + price_1 +
                ", price_2=" + price_2 +
                ", out=" + out +
                '}';
    }
}

class Price {
    private String material_name;
    private int amount;

    public String getMaterial_name() {
        return material_name;
    }

    public void setMaterial_name(String material_name) {
        this.material_name = material_name;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
}


我想使用“第一个”对象,如下所示:

TradeItem[] trade_list = tradeList.get_trade_items(trade_list_name);


得到正确的数据。

n3h0vuf2

n3h0vuf21#

您的JSON字符串不包含 dynamicItems 字段。这将实现您的目标tradeList.getDynamicItems().get("first")

Gson gson = new GsonBuilder().create();
Type type = new TypeToken<Map<String, List<TradeItem>>>() {}.getType();
Map<String, List<TradeItem>> dynamicItems = gson.fromJson(jsonString, type);
TradeList tradeList = new TradeList();
tradeList.setDynamicItems(dynamicItems);

字符串

相关问题