如何将POJO转换为Android上可读的字符串进行调试? [英] How convert a POJO into a readable string on Android for debugging?

查看:79
本文介绍了如何将POJO转换为Android上可读的字符串进行调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下纯文本JSON对象

  class Chat {

String id;
字符串标题;
列表< String>消息;


$ / code>

如何覆盖 toString 为了使其仅为人类可读的仅用于调试

解决方案

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

  class Chat {

private static final GSON = new GSON ();

字符串ID;
字符串标题;
列表< String>消息;

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

}


Given a the following plain-text JSON object

class Chat {

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

}

how to I override toString in order to it be human readable only for debugging?

解决方案

You could serialize it into a JSON string:

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();
    }

}

这篇关于如何将POJO转换为Android上可读的字符串进行调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆