使用Gson优雅地处理嵌套的json对象? [英] Using Gson to elegantly handle nested json objects?

查看:124
本文介绍了使用Gson优雅地处理嵌套的json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gson解析来自Android上的服务器的响应。每个响应对我来说都有一些无用的数据,这使我的Gson模型变得复杂。这是返回的json的一般层次:

 响应:{
date:1406253006807,
otherUselessData1: 这是一些无用的数据,
otherUselessData2:这是一些无用的数据,

usefulJsonObject:{< ----这真的是我关心的


$ b $ / code>

以上或与 usefulJsonObject 我真的可以没有。对每个请求都返回无用数据,实际响应作为 usefulJsonObject 嵌入到下面。这不会是一个大问题,但它真的让我的gson模型对象混乱。



例如:
假设我有3个要求: A,B和C STRONG>。对于每个响应,我似乎至少需要创建3个自定义类。

  public class ResponseA {

@SerializedName(response)ResponseObjectA responseObject;

public static class ResponseObjectA {
@SerializedName(usefulJsonObject)UsefulObjectA usefulObject;


public static class UsefulObjectA {
}

}

我尝试了一些解决方案,但我还没有发现任何优雅的东西,不会为我的过程添加额外的步骤。我使用retrofit来完成我的http请求,它真的很好,它只是将完全解析的gson对象返回给我。我想过其他的解决方案,比如让有用的对象只是一个JsonElement,然后在第一次回来之后做第二次gson调用。再次,不理想。



我只想知道我是否错过了一些东西。当然,我不是唯一遇到这种情况的人,所以我想问问别人会如何处理。

/ div>

我从来没有找到一个处理Gson的优雅方式。我尝试了泛型的几个选项,所有这些都没有工作,或留下了一些想要的。



由于我使用Retrofit,我决定覆盖GsonConverter,只是滤除我所有请求中不必要的信息。它最终没有那么灵活,因为我不能使用相同的Retrofit网络接口来调用其他服务器,但我并没有真正这样做,而且它也有2轮json解析调用(MEH)。

  public class CustomGsonConverter extends GsonConverter {

私人Gson mGson;

public CustomGsonConverter(Gson gson){
super(gson);
this.mGson = gson;


public CustomGsonConverter(Gson gson,String encoding){
super(gson,encoding);
this.mGson = gson;
}

@Override public Object fromBody(TypedInput body,Type type)throws ConversionException {
try {
CustomResponse customResponse = mGson.fromJson(new InputStreamReader(body。 in()),CustomResponse.class);
返回mGson.fromJson(customResponse.responseObject.data,type);
} catch(IOException e){
throw new ConversionException(e);



public static class CustomResponse {

@SerializedName(rsp)ResponseObject responseObject;

public static class ResponseObject {

// @SerializedName(date)long date;

@SerializedName(data)JsonElement数据;

}

}

}

也许有一种更好的方式,我只是没有意识到。


I'm using Gson to parse responses from a server on Android. Each response has some useless (to me) data on it that complicates my Gson models. Here is the general hierarchy of json returned:

response: {
  date: 1406253006807,
  otherUselessData1: "This is some useless data",
  otherUselessData2: "This is some useless data",

  usefulJsonObject: {   <---- This is really the object that I care about
  }

}

Everything above or at the same level as usefulJsonObject I could really do without. The useless data is returned for every request, and the actual response is embedded beneath as the usefulJsonObject. This wouldn't be a big problem but it's really cluttering up my gson model objects.

For example: Let's say I have 3 requests I can make: A, B, and C. For each response it seems I need to make a minimum of 3 custom classes.

public class ResponseA {

  @SerializedName("response") ResponseObjectA responseObject;

  public static class ResponseObjectA {
    @SerializedName("usefulJsonObject") UsefulObjectA usefulObject; 
  }

  public static class UsefulObjectA {
  }

}

I've tried a few solutions, but I haven't found anything elegant that wouldn't add an extra step to my process. I'm using retrofit to do my http requests and it's really nice that it just returns the fully parsed gson object to me. I've thought of other solutions like having the useful object just be a JsonElement and then doing a 2nd gson call after the first comes back. Again, not ideal.

I just wanted to know if I was missing something. Surely I'm not the only one who's encountered something like this, and so I thought I'd ask how other people would handle something like this.

解决方案

I never found an elegant way dealing with just Gson. I tried several options with Generics, all of which didn't work or left something to be desired.

Since I'm using Retrofit, I decided to override the GsonConverter, and just filter out the unnecessary information from all my requests. It ends up not being as flexible, as in I can't use the same Retrofit network interface for calls to other servers, but I'm not really doing that, and it also has the down side of having 2 rounds of json parsing calls (meh). You could probably do this more efficiently, but this is working for me for now.

public class CustomGsonConverter extends GsonConverter {

  private Gson mGson;

  public CustomGsonConverter(Gson gson) {
    super(gson);
    this.mGson = gson;
  }

  public CustomGsonConverter(Gson gson, String encoding) {
    super(gson, encoding);
    this.mGson = gson;
  }

  @Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
    try {
      CustomResponse customResponse = mGson.fromJson(new InputStreamReader(body.in()), CustomResponse.class);
      return mGson.fromJson(customResponse.responseObject.data, type);
    } catch (IOException e) {
      throw new ConversionException(e);
    }
  }

  public static class CustomResponse {

    @SerializedName("rsp") ResponseObject responseObject;

    public static class ResponseObject {

//    @SerializedName("date") long date;

      @SerializedName("data") JsonElement data;

    }

  }

}

Maybe there is a better way that I'm just not realizing.

这篇关于使用Gson优雅地处理嵌套的json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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