使用带有冒号的GSON创建JSON作为字段名称的一部分 [英] Create JSON using GSON with a colon as part of a field's name

查看:140
本文介绍了使用带有冒号的GSON创建JSON作为字段名称的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一组需要转换成json的类(pojos)。因为我有一个约束条件,即json字段名称遵循某种格式,我已经将gson作为我选择的库,因为它允许注释字段名称。

因此,我有json字段名称,例如 asset_type preview_image_thumbnail 等等,任何元数据字段必须具有元数据:<元数据字段名称> 格式。

所以,这是归结于我的元数据:标记元数据:根据gson的说法,至少注释不会被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:测试网站

这里是我尝试转换我的课程时遇到的异常到json:

  java.lang.IllegalArgumentException:metadata:标记不是有效的JSON字段名称。 

以下是我想转换的类:

  public class RemoteAsset {

/ **视频名称** /
私有字符串名称;

/ **视频描述** /
私人字符串描述;
$ b $ ** **视频资产类型** /
@SerializedName(asset_type)
private String assetType;

/ **视频的持续时间(以毫秒计)** /
私人持续时间;

/ **视频的缩略图预览网址** /
@SerializedName(preview_image_url)
private String previewImageUrl;
$ b $ **视频的OpenCms结构ID ** /
@SerializedName(external_id)
private String externalId;

**视频的各种流式URL ** /
@SerializedName(stream_urls)
私有StreamUrls streamUrls;
$ b $ ** **视频的标签,以逗号分隔** /
@SerializedName(metadata:tags)
private String metadataTags;

/ **视频的主网站** /
@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( ,append(this.duration),
.append(,previewImageUrl =)。append(this。 previewImageUrl)
.append(,externalId =)。append(this.externalId)
.append(,streamUrls =)。append(this.streamUrls).append(]);
return builder.toString();
}
}


解决方案

问题在于那些不能直接映射到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; < - 你可以做到!
@SerializedName(some:field)
字符串someField;
}

输出:


{some:field:foo}


I've created a set of classes (pojos) that need to be transformed into json. because i have a constraint that json field names adhere to a certain format, i've settled on gson as my library of choice, as it allows for annotations of field names.

so, i have json field names like asset_type, preview_image_thumbnail, etc. along with that, any metadata fields must have the format, metadata:<metadata-field-name>.

so, what this comes down to is that my metadata:tags and metadata:site annotations will not be transformed by gson, since they are not valid json field names, according to gson, at least.

all works well, except for those darned metadata field names. my goal is to have output like the following:

{
  "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"
}

here is the exception i get when attempting to transform my class to json:

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

and here is the class i want to transform:

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

解决方案

The problem is that those can't be mapped directly to Java variables because you can't have a colon in a variable name. You need to use the Gson @SerializedName annotation. The following works at least in Gson version 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;
}

Output:

{"some:field":"foo"}

这篇关于使用带有冒号的GSON创建JSON作为字段名称的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆