2013-02-11 79 views
2

我創建了一組需要轉換爲json的類(pojos)。因爲我有一個約束條件,即json字段名稱遵循某種格式,我已經將gson作爲我選擇的庫,因爲它允許註釋字段名稱。用冒號作爲字段名稱的一部分使用GSON創建JSON

因此,我有json字段名稱,如asset_typepreview_image_thumbnail等,以及任何元數據字段必須具有格式metadata:<metadata-field-name>。根據gson的說法,至少,我的metadata:tagsmetadata:site註釋不會被gson轉換,因爲它們不是有效的json字段名稱。

一切正常,除了那些夢幻般的元數據字段名稱。我的目標是有類似以下的輸出:

{ 
    "name": "Test Remote Asset", 
    "description": "test-remote-asset", 
    "asset_type": "remote_asset", 
    "duration": 172360, 
    "stream_urls": { 
    "flash": "http://www.test-site.com/videos/a-video.flv", 
    "iphone": "http://www.test-site.com/videos/a-video.3gp", 
    "ipad": "http://www.test-site.com/videos/a-video.3gp", 
    "source_file": "http://www.test-site.com/videos/a-video.mp4" 
    }, 
    "metadata:tags": "tag1,tag2,tag3", 
    "metadata:site": "test-site" 
} 

這裏是例外嘗試我的課轉化爲JSON當我得到:

java.lang.IllegalArgumentException: metadata:tags is not a valid JSON field name. 

,這裏是課堂上,我想變換:

public class RemoteAsset { 

/** The video's name **/ 
private String name; 

/** The video's description **/ 
private String description; 

/** The video asset type **/ 
@SerializedName("asset_type") 
private String assetType; 

/** The video's duration, in milliseconds **/ 
private long duration; 

/** The video's thumbnail preview URL **/ 
@SerializedName("preview_image_url") 
private String previewImageUrl; 

/** The video's OpenCms Structure ID **/ 
@SerializedName("external_id") 
private String externalId; 

/** The video's various streaming URLs **/ 
@SerializedName("stream_urls") 
private StreamUrls streamUrls; 

/** The video's tags, coma-separated **/ 
@SerializedName("metadata:tags") 
private String metadataTags; 

/** The video's host site **/ 
@SerializedName("metadata:site") 
private String metadataSite; 

public String getMetadataTags() { 
    return metadataTags; 
} 

public void setMetadataTags(String metadata_tags) { 
    this.metadataTags = metadata_tags; 
} 

public String getMetadataSite() { 
    return metadataSite; 
} 

public void setMetadataSite(String metadata_site) { 
    this.metadataSite = metadata_site; 
} 

public RemoteAsset() { 

    this.streamUrls = null; 
    this.assetType = null; 
    this.previewImageUrl = ""; 
    this.metadataSite = ""; 
    this.metadataTags = ""; 
    this.externalId = ""; 
    this.description = ""; 
    this.duration = 0L; 
    this.name = ""; 
} 

public String getName() { 
    return this.name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getDescription() { 
    return this.description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getAssetType() { 
    return this.assetType; 
} 

public void setAssetType(ASSET_TYPE asset_type) { 
    this.assetType = asset_type.getTypeName(); 
} 

public long getDuration() { 
    return this.duration; 
} 

public void setDuration(long duration) { 
    this.duration = duration; 
} 

public String getPreviewImageUrl() { 
    return this.previewImageUrl; 
} 

public void setPreviewImageUrl(String preview_image_url) { 
    this.previewImageUrl = preview_image_url; 
} 

public String getExternalId() { 
    return this.externalId; 
} 

public void setExternalId(String external_id) { 
    this.externalId = external_id; 
} 

public StreamUrls getStreamUrls() { 
    return this.streamUrls; 
} 

public void setStreamUrls(StreamUrls stream_urls) { 
    this.streamUrls = stream_urls; 
} 

@Override 
public String toString() { 
    StringBuilder builder = new StringBuilder(); 
    builder.append("RemoteAsset [name=").append(this.name) 
      .append(", description=").append(this.description) 
      .append(", assetType=").append(this.assetType) 
      .append(", duration=").append(this.duration) 
      .append(", previewImageUrl=").append(this.previewImageUrl) 
      .append(", externalId=").append(this.externalId) 
      .append(", streamUrls=").append(this.streamUrls).append("]"); 
    return builder.toString(); 
} 
} 

回答

2

問題是那些無法直接映射到Java變量,因爲您不能在變量名中包含冒號。您需要使用Gson @SerializedName註釋。至少在GSON版本2.2.2以下工作:

public static void main(String[] args) 
{ 
    String json = "{\"some:field\":\"foo\"}"; 
    Gson gson = new Gson(); 

    MyClass mc = gson.fromJson(json, MyClass.class); 

    json = gson.toJson(mc); 
    System.out.println(json); 
} 

class MyClass 
{ 
    // String some:field; <- You can do that! 
    @SerializedName("some:field") 
    String someField; 
} 

輸出:

{ 「一些:場」: 「富」}

+0

是啊,我知道得很清楚,但根據gson的說法,我也不能在註釋中留下冒號。我編輯了我的原始問題,包括我想轉換爲json的類,使用gson。 – liltitus27 2013-02-13 16:12:56

+1

我不知道你使用的是什麼版本的Gson,但我發佈的上面的例子是可運行的,並生成我發佈的輸出。它不會拋出異常。也可以將您的課程複製/粘貼到我的IDE中,Gson沒有任何問題。這與目前的Gson 2.2.2 – 2013-02-13 16:27:21

+0

好,就是這樣 - 我正在使用1.7.1。 我感謝您的幫助,布萊恩,謝謝! – liltitus27 2013-02-14 15:30:03

相關問題