递归打印对象详细信息
如何递归打印对象的内容?
How do I print the content of an object recursively?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何递归打印对象的内容?
How do I print the content of an object recursively?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
您可以通过在所有类中重写
toString
来递归打印它。如果您想要像
printObjectRecursively(Object o)
这样的方法,您需要深入研究反射、获取字段、使用printObjectRecursively(someField)
递归地打印它们的名称和内容。示例:
打印:
基于反射的递归打印方法可以写成这样
You can print it recursively by overriding
toString
in all your classes.If you want to have a method like
printObjectRecursively(Object o)
you need to dive into reflection, fetch the fields, print their name and content recursively usingprintObjectRecursively(someField)
.Example:
Prints:
A reflection-based recursive print method could be written something like this
Apache Common Lang 包含
ToStringBuilder
班级。您可以使用ToStringStyle
对象定义不同的样式。单行递归:
树状递归:
Apache Common Lang contains
ToStringBuilder
class. You can define different style withToStringStyle
object.Single line recursion:
Tree-like recursion:
我偶尔使用 XStream 转储对象的 JSON 表示形式,取得了巨大成功。它会递归对象,并且大多数时候似乎只是做您希望它做的事情。而且它超轻。例子:
I've had great success doing this on a casual basis using XStream to dump JSON representations of objects. It recurses down objects and just seems to do what you want it to do most of the time. And it's super lightweight. Example:
您正在寻找类似于 PHP 的 var_dump 的内容,看看这个问题是否有任何帮助: PHP var_dump 的 Java 等价物是什么?
另外,看看反射:http://java.sun.com/developer/technicalArticles/ALT/Reflection/
You are looking for something similar to PHP's var_dump, see if this question is of any help: What is the Java equivalent of PHP var_dump?
Also, have a look at reflection: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
使用序列化库之一,例如 Jackson (JSON)。
这会将所有内容转储为纯文本。如果您使用支持 Javascript 的编辑器来美化内容,运气好的话,您实际上可能会从中理解一些内容。
如果很多对象不可序列化,你可能会得到很多可读的乱码,这对任何事情都没有帮助,不幸的是,
YMMV
Use one of the serialization libraries like Jackson (JSON).
This dumps everything in plain text. If you use a Javascript capable editor to prettify the content, with a bit of luck, you might actually make some sense out of it.
If a lot of the objects are not serializable, you might end up with a lot of readable gibberish which will not help anything, unfortunately,
YMMV
在某些情况下,使用Gson打印对象很容易。但有时gson不起作用。
在这里,我只是改进 aioobe 对这种情况的回答:
In some cases it is easy to use Gson to print object. But sometimes gson does not work.
Here I just improve aioobe' answer for this case:
您应该为您的类实现
toString
方法 - 它将打印有关类成员的信息 - 通常使用它们的toString
方法。 |然后你只需迭代集合并调用每个项目的toString
You should implement the
toString
method for your classes - it will print the information about the class members - usually using theirtoString
methods. |Then you jut iterate through the collection and calltoString
of each item您真的需要打印这些信息吗?也许调试期间观看就足够了?
Do you really need print this informations out? Maybe watch during debbuging will be enough?
您可以重写 toString 方法。
示例:
You can override the toString method.
Example: