根据 json 响应创建 java 模型
Create a java model from json response
我正在尝试为黑色素瘤检测应用程序中的 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");
}
}
有办法吗?
你可以用这个link生成pojo
https://json2csharp.com/json-to-pojo
这就是您的 pojo 在单个文件中的样子
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;
}
实际上您的 java 模型运行良好。
您可以通过以下方式访问每个 label/probability 作为列表元素:
serverResponse.getPredictions().get(0).getLabel()
serverResponse.getPredictions().get(0).getProbability()
(这应该给你第一个标签-概率元素对)。
如果您的响应预测列表中总是有 2 个元素(一个用于黑色素瘤,一个用于非黑色素瘤),您可以轻松地使用 get(0) 和 get(1) 对其进行硬编码。
我正在尝试为黑色素瘤检测应用程序中的 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");
}
}
有办法吗?
你可以用这个link生成pojo
https://json2csharp.com/json-to-pojo
这就是您的 pojo 在单个文件中的样子
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;
}
实际上您的 java 模型运行良好。 您可以通过以下方式访问每个 label/probability 作为列表元素:
serverResponse.getPredictions().get(0).getLabel()
serverResponse.getPredictions().get(0).getProbability()
(这应该给你第一个标签-概率元素对)。
如果您的响应预测列表中总是有 2 个元素(一个用于黑色素瘤,一个用于非黑色素瘤),您可以轻松地使用 get(0) 和 get(1) 对其进行硬编码。