2016-10-24 3 views
3

私は、次のJSONレスポンスを持っている:jsonレスポンスフィールドをjavaのユーザー定義フィールドに変更するにはどうすればよいですか?

{ 
"Labels": { 
        "com.prop.vendor": "Acme", 
        "com.example.license": "GPL", 
        "com.example.version": "1.0" 
      } 
} 

は今、私は私のJavaクラスでこれらの値を処理したいです。私が持っている問題は、com.example.versionのようなフィールドの場合です。pojoは、データ型と同じものを使用して生成されています。同様に、

private String com.example.version 

私はコンパイル時にエラーが発生します。 したがって、 "com.example.version"を指すフィールド "String version"を作成します。 そして、@ JsonPropertyと@JsonSetterを使用して試しました。

Labels.java

public class Labels 
{ 
    @JsonProperty("com.example.version") 
    private String version; 

    @JsonProperty("com.example.vendor") 
    private String vendor; 

    @JsonProperty("com.example.license") 
    private String license; 

    public String getVersion() 
    { 
     return version; 
    } 

    public void setVersion(String version) 
    { 
     this.version = version; 
    } 

    public String getVendor() 
    { 
     return vendor; 
    } 

    public void setVendor(String vendor) 
    { 
     this.vendor = vendor; 
    } 

    public String getLicense() 
    { 
     return license; 
    } 

    public void setLicense(String license) 
    { 
     this.license = license; 
    } 

    @Override 
    public String toString() 
    { 
     return "ClassPojo [com.example.version = " + version + ", com.example.vendor = " + vendor + ", com.example.license = " 
       + license + "]"; 
    } 
} 

しかし、毎回私はこのエラーを取得しています:

私はそれを使用していますどのように
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Labels" (class com.csc.agility.adapters.service.docker.pojo.Labels), not marked as ignorable (3 known properties: "com.example.license", "com.example.vendor", "com.example.version"]) 
at [Source: { 
"Labels": { 
        "com.example.vendor": "Acme", 
        "com.example.license": "GPL", 
        "com.example.version": "1.0" 
      } 
}; line: 2, column: 12] (through reference chain: com.csc.agility.adapters.service.docker.pojo.Labels["Labels"]) 

ObjectMapper objMap = new ObjectMapper(); 
String jsonInString = 
       "{\n\"Labels\": {\n      \"com.example.vendor\": \"Acme\",\n      \"com.example.license\": \"GPL\",\n      \"com.example.version\": \"1.0\"\n    }\n}"; 

Label label = objMap.readValue(jsonInString, Label.class); 
String licence = label.getLicense(); 

誰でもに私を指すことができます正しいJSONアノテーションを使用するか、それを達成するための他の方法?これはあなたのコードの使用では

public class Response { 
@JsonProperty("Labels") 
private Labels labels; 
//setter 
//getter 
} 

- -

+0

カスタムシリアライザを試しましたか? –

+0

"Labels"もマッピングする必要があります –

+0

はい、オブジェクトマッパーを使用してマッピングしています。編集をご覧ください。 – Siddharth

答えて

2

はあなたのmapperとを設定する必要があります

mapper.configure(Feature.UNWRAP_ROOT_VALUE, true);(バージョン1.9用)

または

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);(バージョン2以上の場合)。

私は上記のプロパティを使用してJSONを試してみましたが、すべてが正常に動作します:

String data = "{\"Labels\": {\"com.example.vendor\": \"Acme\",\"com.example.license\": \"GPL\",\"com.example.version\": \"1.0\"}}"; 

ObjectMapper mapper = new ObjectMapper(); 
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); 
Labels labels = mapper.readValue(data, Labels.class); 
System.out.println(labels); 

注:JSON文字列では、私はそれを実行可能にするためにcom.example.vendorcom.prop.vendorを変更しました。

+0

マイナーな問題:私はあなたのコードを試していますが、フィーチャクラスのインポートを見つけることができません。インポート行をpingして、それぞれのjarをインポートできますか? – Siddharth

+0

既に上述したように、異なるジャックソン版には異なる構成が必要です。あなたのバージョンをpingしてください。 –

+0

Jackson-annotations 2.7.0 – Siddharth

0

は/デシリアライズシリアライズする1つの以上のPOJO以下のように持っているとラベルの代わりに、このオブジェクトを使用

Response response = objMap.readValue(jsonInString, Response.class); 
String licence = response.getLabels().getLicense(); 
関連する問題