使用GSON和RETROFIT反序列化复杂的JSON对象

时间:2017-10-17 06:26:54

标签: java android retrofit gson

学习android native并坚持使用GSON Deserialize ... 无法解析来自API的深层嵌套JSON对象响应

数据返回:

{
  "parent_type": "some_type",
  "data": {
    "children": [
      {
        "id": "some_id",
        "data": {
          "name": "some_name",
          "desc": "bla bla bla"
        }
      },
      {
        "id": "some_other_id",
        "data": {
          "name": "some_other_name",
          "desc": "other bla bla bla"
        }
      }
    ]
  }
}

我有一个看起来像这样的RETROFIT客户端:

    baseUrl = "https://www.someurl.com/";

    OkHttpClient client = createClientWithInterceptors();

    // create GSON deserializer
    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.registerTypeAdapter(Some.class, new SomeDeserializer());
    Gson myGson = gsonBuilder.create();

    _retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(myGson))
            .build();

GET看起来像这样:

// end points
public interface Somethings {
    @GET("someendpoint/somthings?limit=2")
    Call<List<Something>> getSomething();
}

然后解串器看起来像这样:

    public class SubRedditDeserializer implements JsonDeserializer<List<Something>> {
        @Override
        public List<Something> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

        // cant figure out how to handle this here..

        return something;
        }
     }

还无法弄清楚Something类结构应该是什么样的..它应该是一个something还是一个数组?

最后,当List<Something>

时,我希望它为Something
public class Something {
    public String name;
    public String desc;
}

谢谢

2 个答案:

答案 0 :(得分:1)

  • 您的.valueJSON,因此JSONObject错误。

  • 您的Bean为List<Something>,它是返回Something值的一部分。因此,您可以更改为JSON

尝试使用此bean

<强> ResponseBean

ResponseBean

然后

public class ResponseBean {

/**
 * parent_type : some_type
 * data : {"children":[{"id":"some_id","data":{"name":"some_name","desc":"bla bla bla"}},{"id":"some_other_id","data":{"name":"some_other_name","desc":"other bla bla bla"}}]}
 */

private String parent_type;
private DataBeanX data;

public String getParent_type() {
    return parent_type;
}

public void setParent_type(String parent_type) {
    this.parent_type = parent_type;
}

public DataBeanX getData() {
    return data;
}

public void setData(DataBeanX data) {
    this.data = data;
}

public static class DataBeanX {
    private List<ChildrenBean> children;

    public List<ChildrenBean> getChildren() {
        return children;
    }

    public void setChildren(List<ChildrenBean> children) {
        this.children = children;
    }

    public static class ChildrenBean {
        /**
         * id : some_id
         * data : {"name":"some_name","desc":"bla bla bla"}
         */

        private String id;
        private DataBean data;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public DataBean getData() {
            return data;
        }

        public void setData(DataBean data) {
            this.data = data;
        }

        public static class DataBean {
            /**
             * name : some_name
             * desc : bla bla bla
             */

            private String name;
            private String desc;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getDesc() {
                return desc;
            }

            public void setDesc(String desc) {
                this.desc = desc;
            }
        }
    }
}
}

并在此

中使用
@GET("someendpoint/somthings?limit=2")
Call<ResponseBean> getSomething();

修改

@Override
public ResponseBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

    // cant figure out how to handle this here..

    return something;
}

如果您使用上面的[ { "name": 12345, "desc": "3" }, { "name": 12346, "desc": "2" }, { "name": 12347, "desc": "1" } ] 代码,则可以在代码中使用JSON

答案 1 :(得分:0)

使用addConverterFactory(ScalarsConverterFactory.create()),Response将完整的json返回为String。将String转换为JsonObject,然后将其解析为模型。