android - 如何将 POJO 转换为 Android 上可读的字符串以进行调试?

标签 android json gson

给定以下纯文本 JSON 对象

class Chat {

    String id;
    String title;
    List<String> messages;

}

如何重写toString以使其人类可读仅用于调试

最佳答案

您可以将其序列化为 JSON 字符串:

class Chat {

    private static final GSON = new GSON();

    String id;
    String title;
    List<String> messages;

    public String toString() {
        return BuildConfig.DEBUG ? GSON.toJson(this) : super.toString();
    }

}

关于android - 如何将 POJO 转换为 Android 上可读的字符串以进行调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47837569/

相关文章:

android - java.util.zip.ZipException : duplicate entry

android - Pushwoosh 泄漏我的 Activity

java - 通过转换 gson 将一些特定的表导出为文件

android - 无法使用 Google Cloud Translation API 限制 API key

android - 如何从Google API请求适配器中的持有人中设置图标网址

javascript - 使用 Controller 中定义的范围进行 ng 过滤

c# - 在 C# 中使用 Newtonsoft 反序列化 JSON

java - 如何从JsonNode对象中检索以特定字母开头的JSON元素列表?

java - 使用 GSON 将对象转换为 json 时出现 OutOfMemoryError

jquery - Json 与 java hashmap