JsonArray as empty string parsing issue with retrofit

一世执手 提交于 2019-12-13 01:26:15

问题


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 are jsonArray but you can see that 2 are not having data so coming as empty string.

How to handle this scenario with retrofit +gson.

Json format can't be changed here at server.

is there any way I can achieve it in android?


回答1:


Ok so there are two option you can solve this

Option 1:

JSON which I used as a example

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

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
                }
            }
        }

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

Option 2:

Follow this which is a little bit complex

Option 3:

Write own custom Parsing like this and Handle your response accordingly




回答2:


a json can have a single structure. From the code it is clear that the key is given with 2 types of data

Ideally, it should not give "" when no items. It should give null

Please change the data

If no items,

"des_facet"=null // this is the change on server side. No need to change it on app side

If it has items

"des_facet"=[
    "Workplace Environment"
  ]

instead of

If no items,

"des_facet"=""

If it has items

"des_facet"=[
    "Workplace Environment"
  ]



回答3:


You can use AutoValue with gson plugin and mark the field as nullable which will notify the Autovalue to make this field optional. AZs an example this is how you do it:

@AutoValue
public abstract class NewsResponse{

       public static TypeAdapter<NewsResponse> typeAdapter(Gson  gson){
               return new AutoValue_NewsResponse.GsonTypeAdapter(gson);
       }

       @SerializedName("api_status")
       public abstract String apiStatus();

       @SerializedName("api_text")
       public abstract String success();

       @Nullable
       @SerializedName("errors")
       public abstract ErrorDetails errorDetails();

        @SerializedName("news")
        public abstract List<NewsDetails> newsDetails();
}

you must import both of them see more info about importing at: AutoValue and AutoValue Gson Plugin



来源:https://stackoverflow.com/questions/51798666/jsonarray-as-empty-string-parsing-issue-with-retrofit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!