Print whole structure with single call (like JSON.stringify) in Java?

后端 未结 4 1814
难免孤独
难免孤独 2021-01-21 19:49

How to print any class instance in Java? Similar to JSON.stringify() in Javascript. Not necessary JSON, any format of output will do.

public class User {
    pub         


        
4条回答
  •  伪装坚强ぢ
    2021-01-21 20:24

    You can use Apache's commons-lang's ToStringBuilder.reflectionToString

    Of course, reflection is slow, so only do this with your test code. for normal use, please use eclipse's "Source" menu -> generate toString() (or intellij's generate toString()) which gives you a decent string.

提交回复
热议问题