简体   繁体   English

杰克逊未能显示空数组:内部为null([null])

[英]Jackson failing displaying empty array: null inside ([null] )

I have this class: 我有这个课:

public class MyClass {
public String methodName;
public Object[] argument;

public MyClass(String m,Object[]){...

I want to send an empty argument array : 我想发送一个空的参数数组:

ObjectMapper mapper = new ObjectMapper();
MyClass cls = new MyClass("list_dbs",new Object[1]);
mapper.writeValue(System.out, req);

I get: 我得到:

{"methodName":"list_dbs","argument":[null]} {“ methodName”:“ list_dbs”,“ argument”:[null]}

Why is there that "null" ? 为什么会有“ null”?

Because you created your array 因为您创建了数组

MyClass cls = new MyClass("list_dbs",new Object[1]);

with one element. 一个元素。

So the Object[] is 所以Object[]

[0] = null

If you want an empty array, ie. 如果要一个空数组,即。 one without elements, you need 一个没有元素的,你需要

MyClass cls = new MyClass("list_dbs",new Object[0]);

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

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