lundi 27 avril 2020

How to extract data from a returned json using retrofit and java

Hi I am using retro fit to try and extract data from a json that has been returned. The problem is there are numerous ids returned in the json and i cant figure out how to get the ids listed seperately. Im currently getting a null pointer exception.

This is the response im using:

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "id",
        "version",
        "createdDate",
        "modifiedDate",
        "rie",
        "diagnosisName"
})
public class GetFavouritesReponse {

    @JsonProperty("id")
    private Integer id;
    @JsonProperty("version")
    private Integer version;
    @JsonProperty("createdDate")
    private Object createdDate;
    @JsonProperty("modifiedDate")
    private Object modifiedDate;
    @JsonProperty("rie")
    private Boolean rie;
    @JsonProperty("diagnosisName")
    private String diagnosisName;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("id")
    public Integer getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(Integer id) {
        this.id = id;
    }

    @JsonProperty("version")
    public Integer getVersion() {
        return version;
    }

    @JsonProperty("version")
    public void setVersion(Integer version) {
        this.version = version;
    }

    @JsonProperty("createdDate")
    public Object getCreatedDate() {
        return createdDate;
    }

    @JsonProperty("createdDate")
    public void setCreatedDate(Object createdDate) {
        this.createdDate = createdDate;
    }

    @JsonProperty("modifiedDate")
    public Object getModifiedDate() {
        return modifiedDate;
    }

    @JsonProperty("modifiedDate")
    public void setModifiedDate(Object modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    @JsonProperty("rie")
    public Boolean getRie() {
        return rie;
    }

    @JsonProperty("rie")
    public void setRie(Boolean rie) {
        this.rie = rie;
    }

    @JsonProperty("diagnosisName")
    public String getDiagnosisName() {
        return diagnosisName;
    }

    @JsonProperty("diagnosisName")
    public void setDiagnosisName(String diagnosisName) {
        this.diagnosisName = diagnosisName;
    }

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

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

}

and this is the method im trying to use

@Then("I get the list of favourites with {string} and {string}")
public void iGetTheListOfFavouritesWithAnd(String username, String password) throws Exception {
    GetFavouritesReponse fave = new GetFavouritesReponse();

    String auth = "Bearer " + LoginApi(username,password);
    String list = favecall.getDiagnosisFavourites(true,auth).execute().body().toString();
    System.out.println(list);

    String id = fave.getId().toString();
    System.out.println("Id is:" + id);


}

The call is returning some data but i cant seem to extract anything out of it, here is an example of whats coming back:

INFO: [ {
"id" : 15,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Angina pectoris - Angina pectoris with documented spasm"
}, {
"id" : 14,
"version" : 0,
"createdDate" : null,
"modifiedDate" : null,
"rie" : false,
"diagnosisName" : "Angina pectoris - Unstable angina"

}]

Im basically trying to take the ids out of the returned data and use them separately in another call, i assume ill need a loop of some sorts but i cant seem to even get any id's

Aucun commentaire:

Enregistrer un commentaire