java - 如何在 play 框架中使用 Json.toJson() 将 pojo 转换为 Json 时从 Json 中删除空值字符串?

标签 java json playframework pojo

从pojo转换为json时,如何得到不包含空字符串的结果json?

 class Test{
        public String id;
        public String firstname;
        public String lastname;
        }

        Test test=new Test();
        test.id="1";
        test.firstname="John";

当我们将 test 转换为 json 时:

Json.tojson(test); // using play.libs.Json

结果:

{
    "id":"1",
    "firstname":"John",
    "lastname":null
    }

预期结果:

{
    "id":"1",
    "firstname":"John"
    }

有人可以帮忙解决这个问题吗?谢谢。

最佳答案

您应该注释您的 POJO,例如:

/**
 * Test class annotate to tell Jackson library to NOT include NULL values.
 */
@JsonInclude(Include.NON_NULL)
class Test {
    public String id;
    public String firstname;
    public String lastname;
}

关于java - 如何在 play 框架中使用 Json.toJson() 将 pojo 转换为 Json 时从 Json 中删除空值字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359466/

相关文章:

java - 当没有 kafka 代理运行时,如何出于开发目的禁用 Spring Cloud 流?

c# - 如何在 C# 和 ASP.NET 中反序列化 JSON 值?

java - 如何在 postexecute 方法中引用 JSON onResponse?

java - 在 Play Framework 中根据参数注入(inject)依赖

mongodb - Playframework + Morphia + MongoDb + ElasticSearch = Disater?

Scala Play 文件上传 : Cannot write an instance of views. html.uploadFile.type 到 HTTP 响应

Java 持久性 : Design with it in mind from the beginning or add in later?

java - 通过SOAP操作生成SOAP服务

javascript - 没有得到 Angular Controller 的响应

.net中Newtonsoft.Json.JsonConvert.SerializeObject(Object source,Newtonsoft.Json.JsonSerializerSettings())对应的java代码?