Gson parser class for Json response - Android

非 Y 不嫁゛ 提交于 2019-12-23 05:46:11

问题


i've got response like below,

Sometime the language tag come with array value

{
    "name": "Tony",
    "id":"123",
    "language":["en","fr" ]
}

for some user the language value come with string value.

{
    "name": "Max",
    "id":"124",
    "language":"en"
}

How to create parser class to parse the json response using GSON?


回答1:


Conceptually this is not a valid JSON. The type of "language" should be determined for the very first moment.

If you are sending a list of languages in some cases, it is easy to send a list of ONE element in other cases.

{
   "name": "Max",
    "id":"124",
    "language":["en"]
}

On the other hand, if there is no way to solve this in the correct way you could use this workarround.

public class Model {
    private String name;
    private int id;
}

public class ModelWithLanguageString extends Model{
    private String language;
}
...
public class ModelWithLanguageArray extends Model{
    private List<String> language;
}

Then in your parser

Gson gson = new Gson();
Model model;
try{
   model = gson.fromJson(response, ModelWithLanguageString.class);
}catch(JSONParserException e){
   model = gson.fromJson(response, ModelWithLanguageArray.class);
}

With this solution probably you have some casting problems along your code.




回答2:


if you are using GSON library then try this

public class ResponseBean implement serializable  {

    @SerializedName("name")
    private String name;

    @SerializedName("id")
    private String id;

    @SerializedName("language")
    private List<Language> language;


    public class Language{

    @SerializedName("0")
    private String en;

    @SerializedName("1")
    private String fr;
    }
}



回答3:


This might not be direct answer, but you can handle/achieve this using Jackson Parser without any try catch. By setting objectmapper properties.

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Few other object mappers you should consiter setting,

objectMapper.setSerializationInclusion(Include.NON_NULL);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

GetDetails.java

package com.domain.project;

import java.util.List;

public class GetDetails{
    private String id;
    private List<String> language;
    private String name;

    public String getId(){
        return this.id;
    }
    public void setId(String id){
        this.id = id;
    }
    public List<String> getLanguage(){
        return this.language;
    }
    public void setLanguage(List<String> language){
        this.language = language;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
}

Pass response for parsing.

GetDetails mDetails = objectMapper.readValue(response, GetDetails.class)

To get values.

String mName = mDetails.getName(); // Name
String id = mDetails.getId(); // ID

// Language 
int mLangSize = mDetails.getLanguage().size();
for (int i = 0; i < mLangSize; i++) {
    System.out.println("\nLang : " +  mDetails.getLanguage().get(i).toString());
}

The above will accept both Array & String in language key.

{
    "name": "Tony",
    "id":"123",
    "language":["en","fr" ]
}

.

{
    "name": "Tony",
    "id":"123",
    "language":"en"
}

Finally a small tip, Use http://jsongen.byingtondesign.com/ to generate pojo classes, when there is an array inside array with few modifications, you can get all you pojo classes in a flash.



来源:https://stackoverflow.com/questions/24650378/gson-parser-class-for-json-response-android

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