PHP var_dump 的 Java 等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/301536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:50:13  来源:igfitidea点击:

What is the Java equivalent of PHP var_dump?

javaphp

提问by Jon Cram

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 中的等效项是什么?

采纳答案by serg10

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 toStringmethod. So here you need to override toString()in your Personclass and return the desired state.

它不像 Java 那样成熟,所以你不能免费获得它。它是通过约定而不是语言结构完成的。在所有数据传输类中(甚至可能在您编写的所有类中...),您应该实现一个合理的toString方法。所以在这里你需要toString()在你的Person类中覆盖并返回所需的状态。

There are utilities availablethat help with writing a good toString method, or most IDEs have an automatic toString()writing shortcut.

有一些实用程序可以帮助编写好的 toString 方法,或者大多数 IDE 都有一个自动toString()编写快捷方式。

回答by Harry Lime

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"

此代码将列出Object“o”的字段及其值

Field[] fields = o.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
    System.out.println(fields[i].getName() + " - " + fields[i].get(o));
}

回答by reallyinsane

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() 方法以获取字段的值。看看这个

回答by Chase Seibert

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 的调试器来查看对象内容的可视化表示。

回答by brmiller

I use Jestrwith reasonable results.

我使用Jestr获得了合理的结果。

回答by Daniel

You XML serialization, and you should get a very neat representation even of deeply nested objects.

您进行 XML 序列化,即使是深度嵌套的对象,也应该得到非常简洁的表示。

回答by qmn1711

I found this method to dump object, try this String dump(Object object)

我找到了这个方法来转储对象,试试这个String dump(Object object)

回答by Ali Mamedov

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(将 JSON 转换为 Java 对象的 Java 库,反之亦然)

DownloadIt, 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()的最佳模拟

回答by mattalxndr

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 库。

回答by Necare

I think something similar you could do is to create a simple method which prints the object you want to see. Something like this:

我认为您可以做的类似事情是创建一个简单的方法来打印您想要查看的对象。像这样的东西:

public static void dd(Object obj) { System.out.println(obj); }

It's not the same like var_dump(), but you can get an general idea of it, without the need to go to your debugger IDE.

它与 var_dump() 不同,但您可以大致了解它,而无需转到您的调试器 IDE。