Gson - 将嵌套对象序列化为属性 [英] Gson - Serialize Nested Object as Attributes

查看:171
本文介绍了Gson - 将嵌套对象序列化为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法来转换如何嵌套的对象转换为JSON?我试图创建一个JSON对象来匹配后端。我正在使用Retrofit进行网络连接,它使用Gson将对象转换为JSON。



我无法访问网络调用和转换之间的任何代码,所以我试图找到一种干净的方式来修改对象的转换方式,通过GsonBuilder或Annotations。

  //自动转换为JSON并传入Gson。 
呼叫< myObject>搜索(@Body foo myFoo);






  public class foo {
String text =boo;
bar b = new bar();
}

public class bar {
String other =moo;

结果:

  {text:boo,b{other:moo}} 

预期结果:

  {text:boo,other :moo} 

感谢您的帮助。 :)

解决方案

更新我查看了GsonBuilder,并且您可以使用自定义序列化来完成此操作。您需要覆盖 JsonSerializer< type>

的$ serialize 方法

<如下定义一个类。

  public class FooSerialize implements JsonSerializer< foo> {

@Override
public JsonElement serialize(foo obj,Type foo,JsonSerializationContext context){

JsonObject object = new JsonObject();
String otherValue = obj.b.other;
object.addProperty(other,otherValue);
object.addProperty(text,obj.text);
返回对象;


$ / code>

创建 gson 对象如下。

  Gson gson = new GsonBuilder()。registerTypeAdapter(foo.class,new FooSerialize() ).setPrettyPrinting()创建(); 

只需转换为Json

  gson.toJson(fooObject); 

瞧!如果它适用于你,则为lmk。我在我的系统上测试过它的工作。忘掉字符串重写它被称为Json到Obj的转换。这只是需要处理反序列化以反对的序列化。寻找在线资源以获得类似线路的想法。

替代解决方案将仅为JSON转换目的定义虚拟pojos。在发送使用setter将值分配给pojo对象并在pojo上使用gson反之亦然或者上面的解决方案来为您需要的类定制序列化和反序列化。

Is there an easy way to convert how a nested Object is converted to JSON? I'm trying to just create one JSON object to match the back-end. I'm using Retrofit for my networking, which converts an Object to JSON with Gson.

I don't have access to any code between network call and the convertion, so I'm trying to find a clean way to modify how the Object is converted, either through the GsonBuilder, or Annotations.

// Automatically converted to JSON with passed in Gson.
Call<myObject> search( @Body foo myFoo ); 


public class foo {
    String text = "boo";
    bar b = new bar();
}

public class bar {
    String other = "moo";
}

Result:

{ "text": "boo", "b" { "other": "moo" } }

Desired Result:

{ "text": "boo", "other": "moo" }

Thanks for your help. :)

解决方案

Update I looked into GsonBuilder and yes you can do it with custom serialization. You need to override serialize method of JsonSerializer<type>

Just define a class as below. here only 2 properties are added.

public class FooSerialize implements JsonSerializer<foo> {

@Override
    public JsonElement serialize(foo obj, Type foo, JsonSerializationContext context) {

         JsonObject object = new JsonObject();
         String otherValue = obj.b.other;
         object.addProperty("other", otherValue );
         object.addProperty("text", obj.text);
         return object;
    }
  }

Create gson object as below.

Gson gson = new GsonBuilder().registerTypeAdapter(foo.class, new FooSerialize()).setPrettyPrinting().create();

Just convert to Json

 gson.toJson(fooObject);

Voila! lmk if it works for you. I tested on my system it worked. Forget about to string override it gets called for Json to Obj conversion. This is only serialization you need to handle deserialize to object as well. Look for online resources to get an idea on similar line.

Alternate solution would be define dummy pojos only for JSON conversion purposes. While sending use setters to assign values to pojo object and use gson on pojo vice versa or above solution to have custom serialize and deserialize for class you need.

这篇关于Gson - 将嵌套对象序列化为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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