2016-05-21 9 views
1

pojoからjsonに変換するときにnull文字列を持たない結果jsonを得るにはどうすればよいですか?再生フレームワークでJson.toJson()を使用してpojoをJsonに変換する際に、null値のStringをJsonから削除する方法はありますか?

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

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

我々はJSONにテストを変換:

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

結果:

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

期待される結果:

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

は、誰かがこれを助けることはできますか?ありがとうございました。

答えて

0

あなたの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; 
} 
0

がnull値を持つプロパティをシリアル化抑制するために、あなたは直接ObjectMapperを設定、または@JsonInclude注釈を利用することができます:

mapper.setSerializationInclusion(Include.NON_NULL); 

または:

@JsonInclude(Include.NON_NULL) 
class Foo 
{ 
    String bar; 
} 
関連する問題