I have the following 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_confirmations": 2
}]
}
}
And i need to dynamically parse BTC and ETH and so on with GSON in the same object. The structure I need in the java class would be the following:
private String symbol; //this would be BTC or ETH or MATIC
private String full_name;
Thanks in advantage!
CodePudding user response:
Use HashMap
at the high level and then store the other JSON properties in other 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;
}
}
Here in Network
class default is reserved keyword @SerializedName("default")
that is being handled this way
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;
}
}
Code to Test
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()));
}
}