在使用GSONparsingJSON时使用Enums
这与我在前面提到的一个前面的问题有关
使用Gson的JSONparsing
我试图parsing相同的JSON,但是现在我已经改变了我的一些类。
{ "lower": 20, "upper": 40, "delimiter": " ", "scope": ["${title}"] } 我的课目前看起来像:
 public class TruncateElement { private int lower; private int upper; private String delimiter; private List<AttributeScope> scope; // getters and setters } public enum AttributeScope { TITLE("${title}"), DESCRIPTION("${description}"), private String scope; AttributeScope(String scope) { this.scope = scope; } public String getScope() { return this.scope; } } 
这段代码抛出一个exception,
 com.google.gson.JsonParseException: The JsonDeserializer EnumTypeAdapter failed to deserialized json object "${title}" given the type class com.amazon.seo.attribute.template.parse.data.AttributeScope at 
这个例外是可以理解的,因为根据我以前的问题的解决scheme,GSON期望Enum对象实际上被创build为
 ${title}("${title}"), ${description}("${description}"); 
但是由于这在语法上是不可能的,所以推荐的解决scheme是什么?
从Gson的文档 :
Gson为枚举提供了默认的序列化和反序列化…如果您希望更改默认表示,可以通过GsonBuilder.registerTypeAdapter(Type,Object)注册types适配器来实现。
以下是一个这样的方法。
 import java.io.FileReader; import java.lang.reflect.Type; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; public class GsonFoo { public static void main(String[] args) throws Exception { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(AttributeScope.class, new AttributeScopeDeserializer()); Gson gson = gsonBuilder.create(); TruncateElement element = gson.fromJson(new FileReader("input.json"), TruncateElement.class); System.out.println(element.lower); System.out.println(element.upper); System.out.println(element.delimiter); System.out.println(element.scope.get(0)); } } class AttributeScopeDeserializer implements JsonDeserializer<AttributeScope> { @Override public AttributeScope deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { AttributeScope[] scopes = AttributeScope.values(); for (AttributeScope scope : scopes) { if (scope.scope.equals(json.getAsString())) return scope; } return null; } } class TruncateElement { int lower; int upper; String delimiter; List<AttributeScope> scope; } enum AttributeScope { TITLE("${title}"), DESCRIPTION("${description}"); String scope; AttributeScope(String scope) { this.scope = scope; } } 
我想扩大NAZIK / user2724653答案(对于我的情况)。 这是一个Java代码:
 public class Item { @SerializedName("status") private Status currentState = null; // other fields, getters, setters, constructor and other code... public enum Status { @SerializedName("0") BUY, @SerializedName("1") DOWNLOAD, @SerializedName("2") DOWNLOADING, @SerializedName("3") OPEN } } 
 在json文件中,只有一个字段"status": "N", ,其中N = 0,1,2,3  – 取决于状态值。 因此, GSON可以很好地处理嵌套enum类的值。 在我的情况下,我已经parsing了从json数组中的Items列表: 
 List<Item> items = new Gson().<List<Item>>fromJson(json, new TypeToken<List<Item>>(){}.getType()); 
 使用注释@SerializedName : 
 @SerializedName("${title}") TITLE, @SerializedName("${description}") DESCRIPTION 
使用GSON 2.2.2版本,enum将被轻松整理和解组。
 import com.google.gson.annotations.SerializedName; enum AttributeScope { @SerializedName("${title}") TITLE("${title}"), @SerializedName("${description}") DESCRIPTION("${description}"); private String scope; AttributeScope(String scope) { this.scope = scope; } public String getScope() { return scope; } }