javaBean某一个对象返回为NULL或者为""时的处理

后台某个接口返回的数据结构如下所示

{
    "status":"0",
    "content":[
        {
            "id":"23",
            "headpic":"[http://newapp.com/4ddaadae-a0d0-4de3-9ecf-d547db9f6099](http://newapp.myqcloud.com/4ddaadae-a0d0-4de3-9ecf-d547db9f6099)",
            "accountid":"6324",
            "card_id":"6089",
            "sex":"1",
            "nickname":"农民的儿子",
            "reply":{
                "id":"4134",
                "fid":"4006",
                "user_id":"6324",
                "card_id":"0",
                "farm_id":"3813",
                "text":"谢谢评价",//回复内容
                "trendsid":"2743",
                "time":"1553670844"//回复时间
                "read":"0",
                "ztime":null,
                "status":"1"
            }
        }
    ]
}

但是实际返回时,若reply为空字符串,返回的数据如下

{
    "content": [
        
        {
            "accountid": "12196",
            "card_id": "22786",
            "ctime": "1547632828",
            "headpic": "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIsicgDhYpVw5P3yYT88A4BaWTiaa7euaBTktq8icjYER0TrJGj2VHsA1d48Z313jfa8KDyD09m0wdBg/132",
            "id": "4085",
            "nickname": "?",
            "reply": ""
        }
    ],
    "status": "0"
}

此时数据解析时就会出错
需要写一个typeAdapter(如下) 设置到Bean类里

public class ReplyEmptyAdapter extends TypeAdapter<CommentBean.ReplyBean> {


    private TypeAdapter< CommentBean.ReplyBean> mTypeAdapter;



    @Override
    public void write(JsonWriter out, CommentBean.ReplyBean value) throws IOException {
        initAdapter();
        mTypeAdapter.write(out,value);
    }

    @Override
    public  CommentBean.ReplyBean read(JsonReader in) throws IOException {
        JsonToken peek = in.peek();
        if (peek == JsonToken.NULL || peek == JsonToken.STRING) {
            in.skipValue();
            return null;
        }
        initAdapter();
        CommentBean.ReplyBean v = mTypeAdapter.read(in);
        return v;
    }

    private void initAdapter() {
        if (mTypeAdapter==null) {
            ConstructorConstructor constructorConstructor = new ConstructorConstructor(Collections.<Type, InstanceCreator<?>>emptyMap());
            JsonAdapterAnnotationTypeAdapterFactory jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);
            ReflectiveTypeAdapterFactory factory = new ReflectiveTypeAdapterFactory(
                    constructorConstructor, FieldNamingPolicy.IDENTITY, Excluder.DEFAULT, jsonAdapterFactory);
            mTypeAdapter = factory.create(new Gson(), new TypeToken< CommentBean.ReplyBean>() {
            });
        }
    }
}

bean类的对应字段如下所示

@JsonAdapter(value = ReplyEmptyAdapter.class)
    @SerializedName("reply")
    private ReplyBean mReply;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981阅读 16,094评论 2 11
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,537评论 0 5
  • # 一度蜜v3.0协议 --- # 交互协议 [TOC] ## 协议说明 ### 请求参数 下表列出了v3.0版协...
    c5e350bc5b40阅读 683评论 0 0
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,751评论 0 17
  • 找到多年前的一期《百家讲坛》,访谈对象易中天,主持人崔永元。当时,易中天在百家讲坛开讲后名扬四方,一时间围绕...
    颖颖潜行阅读 141评论 0 0