gpt4 book ai didi

android - 如何阻止 Moshi 解析特定的对象属性

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

我的 JSON 响应(来自服务器)具有 JSON 对象的属性,但我不想解析它们,而是想将其中一些保留为 JSON 编码的字符串。

例如:

{
"date": "23-03-2019",
"changed": true,
"data": {
"login": "9999999",
"loginFormatted": "999 99 99",
}
}

这里我想将“数据”属性解析为字符串。我该怎么做? (我使用的是 Retrofit v2.4.0 和 Moshi v1.5.0)

我的响应模型类:

public class Response {
@Json(name = "date")
public long date;
@Json(name = "changed")
public boolean changed;
@Json(name = "data")
public String data;
}

最佳答案

当 Moshi 查看 Response 的层次结构时类,它决定使用 JsonAdapter<String>解析字段 data .所以解决办法就是告诉Moshi不要用JsonAdapter<String>解析它但将任务委托(delegate)给我们的 JsonAdapter .

说话很便宜,这是代码。

class KeepAsJsonString {
public void run() throws Exception {
String json = "" +
"{\n" +
"\"date\": \"23-03-2019\",\n" +
"\"changed\": true,\n" +
"\"data\": {\n" +
" \"login\": \"9999999\",\n" +
" \"loginFormatted\": \"999 99 99\"\n" +
" }\n" +
"}";

Moshi moshi = new Moshi.Builder().add(new DataToStringAdapter()).build();
JsonAdapter<Response> jsonAdapter = moshi.adapter(Response.class);

Response response = jsonAdapter.fromJson(json);
System.out.println(response.data); // {"login":"9999999","loginFormatted":"999 99 99"}
}

static class Response {
@Json(name = "date")
public String date;
@Json(name = "changed")
public boolean changed;
// Ask moshi to forward the intermediate result to some function with a String annotated with @DataString,
// in our case, DataToStringAdapter.fromJson() and DataToStringAdapter.toJson()
@Json(name = "data")
public @DataString String data;
}

@Retention(RUNTIME)
@JsonQualifier
public @interface DataString {
}

static class DataToStringAdapter {
@ToJson
void toJson(JsonWriter writer, @DataString String string) throws IOException {
// Write raw JSON string
writer.value(new Buffer().writeUtf8(string));
}

@FromJson @DataString
String fromJson(JsonReader reader, JsonAdapter<Object> delegate) throws IOException {
// Now the intermediate data object (a Map) comes here
Object data = reader.readJsonValue();
// Just delegate to JsonAdapter<Object>, so we got a JSON string of the object
return delegate.toJson(data);
}
}

public static void main(String[] args) throws Exception {
new KeepAsJsonString().run();
}
}

在 Kotlin 中它看起来像这样:

@JsonQualifier
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION)
annotation class DataString

internal class JsonObjectToStringJsonAdapter {

@ToJson
fun toJson(@DataString s: String): String {
return s
}

@FromJson
@DataString
fun fromJson(reader: JsonReader, adapter: JsonAdapter<Any>): String {
val jsonObject: Any = reader.readJsonValue()!!
return adapter.toJson(jsonObject)
}
}

更新:

正如 Eric Cochran 提到的,当 issue 时,将有一种更有效的方式 ( JsonReader.readJsonString() ) 将 JSON 读取为字符串是固定的。

关于android - 如何阻止 Moshi 解析特定的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55332124/

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