ZookKep
ZookKep

Reputation: 501

Create a java model from json response

I'm trying to create a java model for my json response in a melanoma detection app.

My response looks like this:

{
"success": true,
"predictions": [
    {
        "label": "Non-melanoma",
        "probability": 0.016881238669157028
    },
    {
        "label": "Melanoma",
        "probability": 0.9831187129020691
    }
]
}

I usually go with https://www.jsonschema2pojo.org/ in creating my java model from json, but this time I am getting this:

    -----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Example {

@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("predictions")
@Expose
private List<Prediction> predictions = null;

public Boolean getSuccess() {
return success;
}

public void setSuccess(Boolean success) {
this.success = success;
}

public List<Prediction> getPredictions() {
return predictions;
}

public void setPredictions(List<Prediction> predictions) {
this.predictions = predictions;
}

}
-----------------------------------com.example.Prediction.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Prediction {

@SerializedName("label")
@Expose
private String label;
@SerializedName("probability")
@Expose
private Double probability;

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public Double getProbability() {
return probability;
}

public void setProbability(Double probability) {
this.probability = probability;
}

}

which leads to different files that I don't know how to use later. I'd like to have one response model, like response_model.java to use like this in the app:

        Call<response_model> call = getResponse.uploadFile(fileToUpload, filename);
        call.enqueue((Callback<response_model>)(new Callback<response_model>() {
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                Intrinsics.checkParameterIsNotNull(call, "call");
                Intrinsics.checkParameterIsNotNull(response, "response");
                if (response.isSuccessful()) {
                    Log.v("upload", "response succ");
                    response_model serverResponse = (response_model) response.body();
                    if (serverResponse.getPredictions()!=null) {
                     ((TextView)findViewById(R.id.output_text)).setText(serverResponse.getPredictions().toString());
                    } else {
                        loader.setVisibility(View.GONE);
                        Toast.makeText(getApplicationContext(), "response null",Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.v("Response 1", "wasnt successfull");
                }
            }

Is there a way?

Upvotes: 2

Views: 132

Answers (2)

C-Bk
C-Bk

Reputation: 321

Actually your java model is working just fine. You can access each label/probability as a list element, by:

serverResponse.getPredictions().get(0).getLabel()
serverResponse.getPredictions().get(0).getProbability()

(which should give you the 1st Label-Probability element pair).

If you're always gonna have 2 elements in your response Prediction list (one for melanoma and one for non-melanoma) you can easily hard-code it with get(0) and get(1).

Upvotes: 3

Doctor Who
Doctor Who

Reputation: 792

You can use this link to generate pojo

https://json2csharp.com/json-to-pojo

This is how your pojo will looks like in a single file

package com.test.test;

import java.util.List;

class Prediction{
    public String label;
    public double probability;
}

public class Test{
    public boolean success;
    public List<Prediction> predictions;
}

Upvotes: 1

Related Questions