JsonArray作为空字符串解析问题与改造 [英] JsonArray as empty string parsing issue with retrofit

查看:365
本文介绍了JsonArray作为空字符串解析问题与改造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json,如果有数据,其中1个键作为jsonArray进入,否则作为空字符串进入.进行翻新的gson解析时出现错误.

I have a json in which 1 key is coming as jsonArray if it has data otherwise it is coming as empty string. It is giving error while parsing in gson with retrofit.

  "section": "Technology",
  "subsection": "",
  "title": "Depiction of Amazon Stirs a Debate About Work Culture",
  "abstract": "Details of working conditions at Amazon led to a response from employees, relatives and friends.",
  "url": "http://www.nytimes.com/2015/08/19/technology/amazon-workplace-reactions-comments.html",
  "byline": "By THE NEW YORK TIMES",
  "item_type": "Article",
  "updated_date": "2015-08-18T07:35:33-5:00",
  "created_date": "2015-08-18T07:35:35-5:00",
  "published_date": "2015-08-19T04:00:00-5:00",
  "material_type_facet": "News",
  "kicker": "",
  "des_facet": [
    "Workplace Environment"
  ],
  "org_facet": [
    "Amazon.com Inc"
  ],
  "per_facet": "",
  "geo_facet": "",

des_facet,org_facet,per_facet,geo_facet是jsonArray,但是您可以看到2没有数据,因此以空字符串形式出现.

des_facet , org_facet, per_facet, geo_facet are jsonArray but you can see that 2 are not having data so coming as empty string.

如何通过改型+ gson处理这种情况.

How to handle this scenario with retrofit +gson.

无法在服务器上更改Json格式.

Json format can't be changed here at server.

有什么办法可以在android中实现吗?

is there any way I can achieve it in android?

推荐答案

好,所以有两种方法可以解决这个问题

Ok so there are two option you can solve this

JSON为例

"des_facet": [
        "Workplace Environment"
    ],
    "org_facet": [
        "Amazon.com Inc"
    ],
    "per_facet": ["Akshay"],
    "geo_facet": ""

在模型类中,将这些变量转换为Object类型

In your model class convert those variable to Object type

@Expose
    @SerializedName("geo_facet")
    private Object geo_facet;
    @Expose
    @SerializedName("per_facet")
    private Object per_facet;

然后在要设置数据的位置执行以下操作

then where you want to set data do the following

if (model != null)
        {
            if (model.getGeo_facet() != null || model.getGeo_facet() != "")
            {
                Object arr = model.getGeo_facet();
            }
            if (model.getPer_facet() !=null || model.getPer_facet()!= "")
            {
                Object arr = model.getPer_facet();
                if (arr!=null && arr.toString().length()>0)
                {
                    arr = arr.toString();
                    Log.d("akshay","arr= "+arr);
                    //Do your Stuff or Set data
                }
            }
        }

这是输出= 08-11 16:51:29.830 17951-17951/com.android.example D/akshay:arr = [Akshay]

This is the output= 08-11 16:51:29.830 17951-17951/com.android.example D/akshay: arr= [Akshay]

选项2:

遵循,这有点复杂

编写自己的自定义解析像这样并相应地处理您的响应

Write own custom Parsing like this and Handle your response accordingly

这篇关于JsonArray作为空字符串解析问题与改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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