Doubt return API Json Android

0

I recently read a lot about getting data through an API on Android, but I can not seem to get what's being returned in Json from a specific API shown below.

{
    "Sumario": {
        "Termo": "teste",
        "Tipo": "teste",
        "Editora": "teste",
        "Autor": "teste",
        "Numero": "1456",
        "Ultima Atualizacao": "2018-02-06 01:21:25",
        "Time Zone": "UTC"
    }
}

I would really like only one example of the code needed to get the data.

    
asked by anonymous 06.02.2018 / 17:38

1 answer

1

You can use the library GSON

//Instancia o gson
Gson gson = new Gson();
//Usa o método gson.fromJson para trans formar o json (primeiro argumento) em uma classe (segundo argumento)
ApiSumario apiSumario = gson.fromJson(json, ApiSumario.class);

Classes :

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Sumario"
})
public class Example {

@JsonProperty("Sumario")
public Sumario sumario;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Termo",
"Tipo",
"Editora",
"Autor",
"Numero",
"Ultima Atualizacao",
"Time Zone"
})
public class Sumario {

@JsonProperty("Termo")
public String termo;
@JsonProperty("Tipo")
public String tipo;
@JsonProperty("Editora")
public String editora;
@JsonProperty("Autor")
public String autor;
@JsonProperty("Numero")
public String numero;
@JsonProperty("Ultima Atualizacao")
public String ultimaAtualizacao;
@JsonProperty("Time Zone")
public String timeZone;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
    
27.04.2018 / 15:55