PHP has a var_dump() function which outputs the internal contents of an object, showing an object's type and content.
PHP有一个var_dump()函数,它输出对象的内部内容,显示对象的类型和内容。
For example:
例如:
class Person {
private $firstName;
private $lastName;
public function __construct($firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
}
}
$person = new Person('Jon', 'Smith');
var_dump($person);
will output:
将输出:
object(Person)#1 (2) {
["firstName:private"]=>
string(3) "Jon"
["lastName:private"]=>
string(5) "Smith"
}
What is the equivalent in Java that will do the same?
在Java中,什么是等价的呢?
9 个解决方案
#1
57
It is not quite as baked-in in Java, so you don't get this for free. It is done with convention rather than language constructs. In all data transfer classes (and maybe even in all classes you write...), you should implement a sensible toString
method. So here you need to override toString()
in your Person
class and return the desired state.
它在Java中并不是很成熟,所以你不能免费得到它。它是通过惯例而不是语言结构来完成的。在所有的数据传输类中(甚至可能在您编写的所有类中),您应该实现一个合理的toString方法。因此,您需要在Person类中覆盖toString()并返回所需的状态。
There are utilities available that help with writing a good toString method, or most IDEs have an automatic toString()
writing shortcut.
有一些实用工具可以帮助编写良好的toString方法,或者大多数ide都有一个自动的toString()编写快捷方式。
EDIT: links dead - now available here
.
编辑:链接死亡-现在在这里可用。
#2
27
In my experience, var_dump is typically used for debugging PHP in place of a step-though debugger. In Java, you can of course use your IDE's debugger to see a visual representation of an object's contents.
在我的经验中,var_dump通常用于调试PHP,而不是逐步调试调试器。在Java中,当然可以使用IDE的调试器来查看对象内容的可视化表示。
#3
14
Your alternatives are to override the toString()
method of your object to output its contents in a way that you like, or to use reflection to inspect the object (in a way similar to what debuggers do).
您的替代方法是重写对象的toString()方法,以您喜欢的方式输出其内容,或者使用反射来检查对象(使用类似于调试器的方式)。
The advantage of using reflection is that you won't need to modify your individual objects to be "analysable", but there is added complexity and if you need nested object support you'll have to write that.
使用反射的优点是,您不需要修改您的单个对象以使其“可分析”,但是还有额外的复杂性,如果您需要嵌套对象支持,您将不得不编写它。
This code will list the fields and their values for an Object
"o"
此代码将列出对象“o”的字段及其值
Field[] fields = o.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
System.out.println(fields[i].getName() + " - " + fields[i].get(o));
}
#4
14
I think that the best way to do It, is using google-gson (A Java library to convert JSON to Java objects and vice-versa)
我认为最好的方法是使用google-gson(一个Java库将JSON转换为Java对象,反之亦然)
Download It, add "jar" file to your project
下载它,添加“jar”文件到您的项目中
HashMap<String, String> map = new HashMap<String, String>();
map.put("key_1", "Baku");
map.put("key_2", "Azerbaijan");
map.put("key_3", "Ali Mamedov");
Gson gson = new Gson();
System.out.println(gson.toJson(map));
Output:
输出:
{"key_3":"Ali Mamedov","key_2":"Azerbaijan","key_1":"Baku"}
You can convert any object (arrays, lists and etc) to JSON. I think, that It is the best analog of PHP's var_dump()
您可以将任何对象(数组、列表等)转换为JSON。我认为,它是PHP的var_dump()的最佳类比
#5
7
The apache commons lang package provides such a class which can be used to build up a default toString() method using reflection to get the values of fields. Just have a look at this.
apache commons lang包提供了这样一个类,可以使用反射来构建默认的toString()方法来获取字段的值。看看这个。
#7
2
I found this method to dump object, try this String dump(Object object)
我找到了这个方法来转储对象,尝试这个字符串转储(对象对象)
#8
2
I like to use GSON because it's often already a dependency of the type of projects I'm working on:
我喜欢使用GSON,因为它通常已经是我正在进行的项目类型的依赖:
public static String getDump(Object o) {
return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}
Or substitute GSON for any other JSON library you use.
或者用GSON替换其他JSON库。
#9
0
You XML serialization, and you should get a very neat representation even of deeply nested objects.
您可以进行XML序列化,即使是深度嵌套的对象,您也应该得到一个非常简洁的表示。
#1
57
It is not quite as baked-in in Java, so you don't get this for free. It is done with convention rather than language constructs. In all data transfer classes (and maybe even in all classes you write...), you should implement a sensible toString
method. So here you need to override toString()
in your Person
class and return the desired state.
它在Java中并不是很成熟,所以你不能免费得到它。它是通过惯例而不是语言结构来完成的。在所有的数据传输类中(甚至可能在您编写的所有类中),您应该实现一个合理的toString方法。因此,您需要在Person类中覆盖toString()并返回所需的状态。
There are utilities available that help with writing a good toString method, or most IDEs have an automatic toString()
writing shortcut.
有一些实用工具可以帮助编写良好的toString方法,或者大多数ide都有一个自动的toString()编写快捷方式。
EDIT: links dead - now available here
.
编辑:链接死亡-现在在这里可用。
#2
27
In my experience, var_dump is typically used for debugging PHP in place of a step-though debugger. In Java, you can of course use your IDE's debugger to see a visual representation of an object's contents.
在我的经验中,var_dump通常用于调试PHP,而不是逐步调试调试器。在Java中,当然可以使用IDE的调试器来查看对象内容的可视化表示。
#3
14
Your alternatives are to override the toString()
method of your object to output its contents in a way that you like, or to use reflection to inspect the object (in a way similar to what debuggers do).
您的替代方法是重写对象的toString()方法,以您喜欢的方式输出其内容,或者使用反射来检查对象(使用类似于调试器的方式)。
The advantage of using reflection is that you won't need to modify your individual objects to be "analysable", but there is added complexity and if you need nested object support you'll have to write that.
使用反射的优点是,您不需要修改您的单个对象以使其“可分析”,但是还有额外的复杂性,如果您需要嵌套对象支持,您将不得不编写它。
This code will list the fields and their values for an Object
"o"
此代码将列出对象“o”的字段及其值
Field[] fields = o.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
System.out.println(fields[i].getName() + " - " + fields[i].get(o));
}
#4
14
I think that the best way to do It, is using google-gson (A Java library to convert JSON to Java objects and vice-versa)
我认为最好的方法是使用google-gson(一个Java库将JSON转换为Java对象,反之亦然)
Download It, add "jar" file to your project
下载它,添加“jar”文件到您的项目中
HashMap<String, String> map = new HashMap<String, String>();
map.put("key_1", "Baku");
map.put("key_2", "Azerbaijan");
map.put("key_3", "Ali Mamedov");
Gson gson = new Gson();
System.out.println(gson.toJson(map));
Output:
输出:
{"key_3":"Ali Mamedov","key_2":"Azerbaijan","key_1":"Baku"}
You can convert any object (arrays, lists and etc) to JSON. I think, that It is the best analog of PHP's var_dump()
您可以将任何对象(数组、列表等)转换为JSON。我认为,它是PHP的var_dump()的最佳类比
#5
7
The apache commons lang package provides such a class which can be used to build up a default toString() method using reflection to get the values of fields. Just have a look at this.
apache commons lang包提供了这样一个类,可以使用反射来构建默认的toString()方法来获取字段的值。看看这个。
#6
#7
2
I found this method to dump object, try this String dump(Object object)
我找到了这个方法来转储对象,尝试这个字符串转储(对象对象)
#8
2
I like to use GSON because it's often already a dependency of the type of projects I'm working on:
我喜欢使用GSON,因为它通常已经是我正在进行的项目类型的依赖:
public static String getDump(Object o) {
return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}
Or substitute GSON for any other JSON library you use.
或者用GSON替换其他JSON库。
#9
0
You XML serialization, and you should get a very neat representation even of deeply nested objects.
您可以进行XML序列化,即使是深度嵌套的对象,您也应该得到一个非常简洁的表示。