I'd like to know if there is a way with Gson to get values of different types in a "key-value" array in Json. Here's the example:
{
"events":[
{"event":"comprou-produto",
"timestamp":"2016-09-22T13:57:32.2311892-03:00",
"custom_data":[
{"key":"product_name","value":"Camisa Azul"},
{"key":"transaction_id","value":"3029384"},
{"key":"product_price","value":100}]} ... outros elementos]}
In this example we have the array "custom_data" with the key-value fields, and notice that "product_price" is a field and its value is a double (or int if you prefer), the others being Strings. How can I get these values with Gson. I made a class to try to read this dataset. Following:
public class Event {
private final String event;
private final String timestamp;
private final double revenue;
private final List<CustomData> custom_data;
//Constructor.
public Event(String event, String timestamp, double revenue, List<CustomData> custom_data) {
this.event = event;
this.timestamp = timestamp;
this.revenue = revenue;
this.custom_data = custom_data;
}
//nested class.
public static class CustomData{
private final String key;
private final String value;
public CustomData(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
Note: The revenue field refers to extra information about the set of events that the outermost array has, in some cases like the one in the example I simply ignore it. Thank you.