简体   繁体   English

Java:打印出对象进行调试

[英]Java: Printing out an object for debugging

I would like an easy way to print out a java object, or to say it another way, serialize an object as a string. 我想要一种简单的方法来打印出java对象,或者换句话说,将对象序列化为字符串。 I would like to see the values of all variables contained within the object, and if there are more objects (like a list or whatever) it should print out the stuff in that object as well. 我想查看对象中包含的所有变量的值,如果还有更多对象(如列表或其他对象),它也应该打印出该对象中的内容。 Basically, it would be something like JSON encoding, but JSON doesn't support infinity, and I need to see if there is infinity or NaN in one of the double or float values. 基本上,这类似于JSON编码,但是JSON不支持无穷大,我需要查看double或float值之一中是否有无穷或NaN。

How can I do this? 我怎样才能做到这一点?

您可以使用Gson序列化为JSON,因为它现在支持NaN和+/-无限

More often than not ToStringBuilder.reflectionToString(Object) works great. 通常, ToStringBuilder.reflectionToString(Object)效果很好。 It wont work when doing Maps though. 虽然在做Maps时不会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM