问题
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