如何在 Java 中使用 GSON 动态解析此 json
How to parse this json dynamically with GSON in Java
我有以下 JSON:
{
"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 class 中需要的结构如下:
private String symbol; //this would be BTC or ETH or MATIC
private String full_name;
提前致谢!
在高层使用HashMap
,然后将其他JSON属性存储在其他classes
中
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
class 默认是保留关键字@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()));
}
}
我有以下 JSON:
{
"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 class 中需要的结构如下:
private String symbol; //this would be BTC or ETH or MATIC
private String full_name;
提前致谢!
在高层使用HashMap
,然后将其他JSON属性存储在其他classes
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
class 默认是保留关键字@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()));
}
}