我创建了一组需要转换为json的类(pojos)。因为我有一个约束,即json字段名称遵循某种格式,我已经选择gson作为我选择的库,因为它允许注释字段名称。
所以,我有json字段名称,如asset_type
,preview_image_thumbnail
等,以及任何元数据字段必须具有格式metadata:<metadata-field-name>
。
所以,这归结为我的metadata:tags
和metadata:site
注释不会被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();
}
}
答案 0 :(得分: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;
}
输出:
{&#34;一些:字段&#34;:&#34;富&#34;}