如何在Java中使用GSON动态解析此json

2vuwiymt  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(169)

下面是一个例子:

{
    "BTC": {
        "full_name": "Bitcoin TST",
        "payin_enabled": true,
        "payout_enabled": true,
        "transfer_enabled": true,
        "precision_transfer": "0.00000001",
        "networks": [{
            "network": "BTC",
            "protocol": "OMNI",
            "default": true,
            "payin_enabled": true,
            "payout_enabled": true,
            "precision_payout": "0.00000001",
            "payout_fee": "0.000725840000",
            "payout_is_payment_id": false,
            "payin_payment_id": false,
            "payin_confirmations": 3
        }]
    },
    "ETH": {
        "full_name": "Ethereum TST",
        "payin_enabled": true,
        "payout_enabled": true,
        "transfer_enabled": true,
        "precision_transfer": "0.000000000001",
        "networks": [{
            "network": "ETHTEST",
            "protocol": "",
            "default": true,
            "payin_enabled": true,
            "payout_enabled": true,
            "precision_payout": "0.000000000000000001",
            "payout_fee": "0.003621047265",
            "payout_is_payment_id": false,
            "payin_payment_id": false,
            "payin_confirmaIions": 2
        }]
    }
}

我需要在同一个对象中使用GSON动态解析BTC和ETH等等。我需要的java类结构如下:

private String symbol; //this would be BTC or ETH or MATIC
private String full_name;

提前感谢!

szqfcxe2

szqfcxe21#

在高级别使用HashMap,然后将其他JSON属性存储在其他类中

class CoinProperties {
    private String full_name;
    private boolean payin_enabled;
    private boolean payout_enabled;
    private boolean transfer_enabled;
    private String precision_transfer;
    ArrayList<Network> networks = new ArrayList<>();

    public CoinProperties(String full_name, boolean payin_enabled, boolean payout_enabled, boolean transfer_enabled, String precision_transfer, ArrayList<Network> networks) {
        this.full_name = full_name;
        this.payin_enabled = payin_enabled;
        this.payout_enabled = payout_enabled;
        this.transfer_enabled = transfer_enabled;
        this.precision_transfer = precision_transfer;
        this.networks = networks;
    }
}

这里,在Network类中,default是保留关键字@SerializedName("default"),它是以这种方式处理的

class Network {
    private String network;
    private String protocol;
    @SerializedName("default")
    private boolean default_enabled;
    private boolean payin_enabled;
    private boolean payout_enabled;
    private String precision_payout;
    private String payout_fee;
    private boolean payout_is_payment_id;
    private boolean payin_payment_id;
    private float payin_confirmations;

    public Network(String network, String protocol, boolean default_enabled, boolean payin_enabled, boolean payout_enabled, String precision_payout, String payout_fee, boolean payout_is_payment_id, boolean payin_payment_id, float payin_confirmations) {
        this.network = network;
        this.protocol = protocol;
        this.default_enabled = default_enabled;
        this.payin_enabled = payin_enabled;
        this.payout_enabled = payout_enabled;
        this.precision_payout = precision_payout;
        this.payout_fee = payout_fee;
        this.payout_is_payment_id = payout_is_payment_id;
        this.payin_payment_id = payin_payment_id;
        this.payin_confirmations = payin_confirmations;
    }
}

要测试的代码

public class Demo {

    public static void main(String[] args) {
        String jsonString = "{ \"BTC\": {\n" +
                "        \"full_name\": \"Bitcoin TST\",\n" +
                "        \"payin_enabled\": true,\n" +
                "        \"payout_enabled\": true,\n" +
                "        \"transfer_enabled\": true,\n" +
                "        \"precision_transfer\": \"0.00000001\",\n" +
                "        \"networks\": [{\n" +
                "            \"network\": \"BTC\",\n" +
                "            \"protocol\": \"OMNI\",\n" +
                "            \"default\": true,\n" +
                "            \"payin_enabled\": true,\n" +
                "            \"payout_enabled\": true,\n" +
                "            \"precision_payout\": \"0.00000001\",\n" +
                "            \"payout_fee\": \"0.000725840000\",\n" +
                "            \"payout_is_payment_id\": false,\n" +
                "            \"payin_payment_id\": false,\n" +
                "            \"payin_confirmations\": 3\n" +
                "        }]\n" +
                "    },\n" +
                "    \"ETH\": {\n" +
                "        \"full_name\": \"Ethereum TST\",\n" +
                "        \"payin_enabled\": true,\n" +
                "        \"payout_enabled\": true,\n" +
                "        \"transfer_enabled\": true,\n" +
                "        \"precision_transfer\": \"0.000000000001\",\n" +
                "        \"networks\": [{\n" +
                "            \"network\": \"ETHTEST\",\n" +
                "            \"protocol\": \"\",\n" +
                "            \"default\": true,\n" +
                "            \"payin_enabled\": true,\n" +
                "            \"payout_enabled\": true,\n" +
                "            \"precision_payout\": \"0.000000000000000001\",\n" +
                "            \"payout_fee\": \"0.003621047265\",\n" +
                "            \"payout_is_payment_id\": false,\n" +
                "            \"payin_payment_id\": false,\n" +
                "            \"payin_confirmations\": 2\n" +
                "        }]\n" +
                "    }\n" +
                "}";

        HashMap<String,CoinProperties> coinNameToProperties = new HashMap<>();
        System.out.println(new Gson().fromJson(jsonString,coinNameToProperties.getClass()));
    }
}

相关问题