2

我创建了一组需要转换为 json 的类 (pojos)。因为我有 json 字段名称遵循某种格式的约束,所以我选择 gson 作为我的选择库,因为它允许对字段名称进行注释。

所以,我有 json 字段名称,如asset_type,preview_image_thumbnail等。除此之外,任何元数据字段都必须具有格式 , metadata:<metadata-field-name>

所以,这归结为 mymetadata:tagsmetadata:siteannotations 不会被 gson 转换,因为它们不是有效的 json 字段名称,至少根据 gson。

一切都很好,除了那些该死的元数据字段名称。我的目标是获得如下输出:

{
  "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();
}
}
4

1 回答 1

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;
}

输出:

{“一些:字段”:“foo”}

于 2013-02-11T20:34:02.243 回答