gpt4 book ai didi

java - 需要一个字符串,但结果是 BEGIN_OBJECT 我必须返回字符串

转载 作者:行者123 更新时间:2023-12-01 20:16:24 25 4
gpt4 key购买 nike

类别

 class PrintRequest {
private String data;
private String userImageBase64;
private String organizationImageBase64;
private String fileBase64;
//getters and setters
}

使用PrintRequest printRequest = gson.fromJson(request.getBody(), PrintRequest.class);

json

{ 
"data":{
"nazwaDostawcy":"nazwa dostawcy (opis)",
"nazwa":"Gniazdo przemysłowe, kątowe, 2p+E, 16A, 100...130 V AC, IP 44 ",
"kod":"val",
"kodEan":"val",
"kodWewnrtrzny":"val",
"cennaNetto":"val"
},
"fileBase64":""
}

就像在主题中一样,我明白为什么会收到此错误,但我需要以字符串形式返回data。我怎样才能实现我的目标?

感谢您的帮助!

最佳答案

这是无需创建自定义 gson 序列化的最简单方法。只需重写 toString() 方法

public class PrintRequest {
private Value data;
private String userImageBase64;
private String organizationImageBase64;
private String fileBase64;
//getters and setters
}

public class Value {
String nazwaDostawcy;

@Override
public String toString() {
return "Value{" +
"nazwaDostawcy='" + nazwaDostawcy + '\'' +
", nazwa='" + nazwa + '\'' +
", kod='" + kod + '\'' +
", kodEan='" + kodEan + '\'' +
", kodWewnrtrzny='" + kodWewnrtrzny + '\'' +
", cennaNetto='" + cennaNetto + '\'' +
", kategoria='" + kategoria + '\'' +
", opis='" + opis + '\'' +
", logo1='" + logo1 + '\'' +
", logo2='" + logo2 + '\'' +
'}';
}

String nazwa;
String kod;
String kodEan;
String kodWewnrtrzny;
String cennaNetto;
String kategoria;
String opis;
String logo1;
String logo2;
//getters and setters
}

用法:

PrintRequest printRequest = gson.fromJson(json, PrintRequest.class); System.out.println(printRequest.getData().toString());

更新:

使用自定义 JsonDeserializer 的解决方案

public class CustomJsonDeserializer implements JsonDeserializer<PrintRequest> {

@Override
public PrintRequest deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
PrintRequest printRequest = new PrintRequest();
JsonObject jsonObject = jsonElement.getAsJsonObject();
String dataStr="";
JsonObject jsonObjectData = jsonObject.getAsJsonObject("data");
for (Data data : Data.values()) {
dataStr+=(data.name() + ":" + jsonObjectData.get(data.name()).getAsString()+",\n");
}
printRequest.setData(dataStr);
return printRequest;
}
}


public enum Data {
nazwaDostawcy,
nazwa
}

public class PrintRequest {
private String data;
private String userImageBase64;
private String organizationImageBase64;
private String fileBase64;
//getters and setters

用法:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
gsonBuilder.registerTypeAdapter(PrintRequest.class, new CustomJsonDeserializer());
Gson gson = gsonBuilder.create();
PrintRequest printRequest = gson.fromJson(json, PrintRequest.class);
System.out.println(printRequest.getData());

关于java - 需要一个字符串,但结果是 BEGIN_OBJECT 我必须返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com