Doubt Return API Json Android

Asked

Viewed 64 times

0

I recently read a lot about getting data through an API on Android, but I can’t 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 obtain the data.

  • Example using lib retrofit: https://code.tutsplus.com/tutorials/getting-started-with-retrofit-2--cms-27792

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);
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.