java - 从 json 响应创建 java 模型

标签 java android json retrofit2

我正在尝试为黑色素瘤检测应用程序中的 json 响应创建一个 java 模型。

我的回复如下所示:

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

我通常选择https://www.jsonschema2pojo.org/从 json 创建我的 java 模型,但这一次我得到了这个:

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

}

这会导致我以后不知道如何使用不同的文件。 我想要一个响应模型,例如 response_model.java,以便在应用程序中使用:

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

有办法吗?

最佳答案

实际上你的java模型运行得很好。 您可以通过以下方式访问每个标签/概率作为列表元素:

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

(这应该为您提供第一个标签-概率元素对)。

如果您的响应预测列表中总是有 2 个元素(一个用于黑色素瘤,一个用于非黑色素瘤),您可以使用 get(0) 和 get(1) 轻松对其进行硬编码。

关于java - 从 json 响应创建 java 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67313222/

相关文章:

android - 基于 ASHMEM 的 SHM 替代

android: 我想在 Market 中使用 DRM

sql - Json数组列拆分成行SQL

ruby-on-rails - 从 json 中删除根的 Carrierwave 选项

c++ - "spamming"boost::property_tree::read_json 时发生访问冲突

java - 当我尝试使用 hibernate 将字段插入表时出错

Java Generics - 类本身的子类?

java - Spinner 中奇怪的 NullPointerException

java - 在位置设置按钮和 TextAreas

java - 导致故障转储的 Java 错误的解决方法