gpt4 book ai didi

android - 如何在使用 Retrofit 和 Gson 转换器进行序列化期间防止转义

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:20 25 4
gpt4 key购买 nike

我陷入了沉思,尽我所能去寻找并想出一个可能的解决方案。我已经尝试了大部分可能与我想要实现的目标相关的答案,但仍然没有成功。

所以详细说明我的问题,我收到了来自 API 的响应

{
"profileList": [
{
"id": "7mmfHGLc0MGtZeQNno/WFqDjlAPj26CS",
"name": "Alexandria Victoria Maxene Kluber van de Gr\\\"oot\""
}
]
}

我想要实现的是在不对值进行转义的情况下获取“名称”字段值。因为在我当前的设置中,反序列化过程后我从该响应中得到的是 Alexandria Victoria Maxene Kluber van de Gr\"oot""

我让我的 Retrofit 处理 API 请求和响应,这就是我得到的结果,我目前不想因为特定原因而取消我的处理程序,但我希望有人能指出我正确的方向。这是我的 Retrofit Builder 代码:

Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.create();
m_builder = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson));

提前谢谢你。

最佳答案

您应该尝试在您的POJO 中使用JsonPrimitive。请参见以下示例:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;

import java.io.File;
import java.io.FileReader;
import java.util.List;

public class GsonApp {

public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();

Gson gson = new GsonBuilder().create();

System.out.println(gson.fromJson(new FileReader(jsonFile), Profiles.class));
}
}

class Profiles {

private List<Profile> profileList;

// getters, setters, toString
}

class Profile {

private String id;
private JsonPrimitive name;

// getters, setters, toString
}

上面的 JSON 负载打印示例:

Profiles{profileList=[Profile{id='7mmfHGLc0MGtZeQNno/WFqDjlAPj26CS', name='"Alexandria Victoria Maxene Kluber  van de Gr\\\"oot\""'}]}

关于android - 如何在使用 Retrofit 和 Gson 转换器进行序列化期间防止转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55014744/

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