gpt4 book ai didi

android - 如何反序列化 JSON 对象但将特定字段保留为字符串而不是嵌套对象?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:56 24 4
gpt4 key购买 nike

我在下面粘贴了一个 json 结构。我想使用 Gson 将 json 反序列化为 java POJO,这非常简单,只是我想保留其中一个字段,data,作为字符串类型而不是嵌套对象。

JSON 结构

{
"created_on": "2015-06-04T16:12:04-0700",
"modified_on": "2015-06-04T16:12:09-0700",
"identifier": "sample",
"name": "some name",
"summary": "some summary",
"data": {
"$type": "a_type",
"some_other_stuff": {
"more_stuff": "lorem ipsum"
},
"type2": {
"$type": "another_type",
"other_stuff": {
"event_more_stuff": "lorem ipsum"
}
}
}
}

我的 POJO 看起来像这样:

public class Sample {
private String identifier; // "sample"
private String created_on; // "2015-06-04T16:12:04-0700"
private String modified_on; // "2015-06-04T16:12:09-0700"
private String name; // "some name"
private String summary; // "some summary"
private String data; // "{ \"$type\": ... }"

// getters and setters
}

data 字段应保留为 JSON 格式的字符串。

我已经尝试实现自定义 TypeAdapter 并将该字段作为字符串读取,但它失败了 Expected a string but was BEGIN_OBJECT

另请注意,我也希望在序列化时保持结构 - 这样我就可以将 POJO 序列化回原始 JSON 结构。

编辑自定义TypeAdapter:

public class SampleTypeAdapter extends TypeAdapter<Sample> {
@Override
public void write(JsonWriter out, Sample sample) throws IOException {
out.beginObject();

out.name("identifier").value(sample.getIdentifier());
out.name("name").value(sample.getName());
out.name("data").value(sample.getData());
out.name("summary").value(sample.getSummary());
out.name("modified_on").value(sample.getModifiedOn());
out.name("created_on").value(sample.getCreatedOn());

out.endObject();
}

@Override
public Sample read(JsonReader in) throws IOException {
final Sample sample = new Sample();

in.beginObject();
while (in.hasNext()) {
String nextName = in.nextName();
switch (nextName) {
case "identifier":
sample.setIdentifier(in.nextString());
break;
case "name":
sample.setName(in.nextString());
break;
case "data":
sample.setData(in.nextString()); // <-- fails here
break;
case "summary":
sample.setSummary(in.nextString());
break;
case "modified_on":
sample.setModifiedOn(in.nextString());
break;
case "created_on":
sample.setCreatedOn(in.nextString());
break;
default:
in.skipValue();
break;
}
}
in.endObject();

return sample;
}
}

最佳答案

您可以创建自定义 JsonDeserializer像这样:

public class SampleGSONParserAdapter implements
JsonDeserializer<Sample> {

@Override
public Sample deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

Sample sample = new Sample();
JsonObject sampleJsonObject = json.getAsJsonObject();

sample.setName(sampleJsonObject.get("name").getAsString());

// do the other parsing stuff here..

// getting the data object as a string
sample.setJsonString(sampleJsonObject.get("data").toString());

return sample;
}

}

然后你像这样使用它:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Sample.class, new SampleGSONParserAdapter());
Gson gson = gsonBuilder.create();

不好的部分是它没有你写的那么快,但至少你可以像这样进行自定义解析。

关于android - 如何反序列化 JSON 对象但将特定字段保留为字符串而不是嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30741627/

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