这与我之前在此问过的上一个问题有关
我试图解析相同的JSON,但现在我已经改变了我的类.
{
"lower": 20,
"upper": 40,
"delimiter": " ",
"scope": ["${title}"]
}
Run Code Online (Sandbox Code Playgroud)
我的课现在看起来像:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码抛出异常,
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
Run Code Online (Sandbox Code Playgroud)
异常是可以理解的,因为根据我之前的问题的解决方案,GSON期望实际上将Enum对象创建为
${title}("${title}"),
${description}("${description}");
Run Code Online (Sandbox Code Playgroud)
但由于这在语法上是不可能的,推荐的解决方案是什么,解决方法呢?
val*_*cat 285
我想扩展一点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
}
}
Run Code Online (Sandbox Code Playgroud)
在json文件中,您只有一个字段"status": "N",
,其中N = 0,1,2,3 - 取决于Status值.所以这一切,GSON
适用于嵌套enum
类的值.在我的情况我已经分析的列表Items
,从json
数组:
List<Item> items = new Gson().<List<Item>>fromJson(json,
new TypeToken<List<Item>>(){}.getType());
Run Code Online (Sandbox Code Playgroud)
Pro*_*uce 51
从Gson的文档:
Gson为Enums提供默认序列化和反序列化...如果您希望更改默认表示,可以通过GsonBuilder.registerTypeAdapter(Type,Object)注册类型适配器来实现.
以下是一种这样的方法.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 31
使用注释@SerializedName
:
@SerializedName("${title}")
TITLE,
@SerializedName("${description}")
DESCRIPTION
Run Code Online (Sandbox Code Playgroud)
使用GSON 2.2.2版枚举将很容易编组和解组.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码片段消除了Gson.registerTypeAdapter(...)
使用@JsonAdapter(class)
自Gson 2.3以来可用的注释对explicit的需要(请参阅注释pm_labs)。
@JsonAdapter(Level.Serializer.class)
public enum Level {
WTF(0),
ERROR(1),
WARNING(2),
INFO(3),
DEBUG(4),
VERBOSE(5);
int levelCode;
Level(int levelCode) {
this.levelCode = levelCode;
}
static Level getLevelByCode(int levelCode) {
for (Level level : values())
if (level.levelCode == levelCode) return level;
return INFO;
}
static class Serializer implements JsonSerializer<Level>, JsonDeserializer<Level> {
@Override
public JsonElement serialize(Level src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src.levelCode);
}
@Override
public Level deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
try {
return getLevelByCode(json.getAsNumber().intValue());
} catch (JsonParseException e) {
return INFO;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
76846 次 |
最近记录: |