我正在尝试将JSON对象(来自API)反序列化到各自的Java对象中。我正在使用gson库(适用于Android)。问题是我已经尝试了好几天并且没有运气测试了很多选项,所以我真的很感激这一点的帮助。
*检索到JSON。我从API收到的所有JSON都遵循以下结构:
{
"status_code": 200,
"status_message": "Here the status message",
"result": {
"success": true,
"internalStatusCall": 1,
"response":
/* Can be a JSON object or a JSON array of objects */
}
}
由于收到的所有JSON的结构都相似,我想利用它并将对象扩展为从JSON映射。
我会考虑像:
public class TABaseObject {
@SerializedName("status_code")
protected int status_code;
@SerializedName("status_message")
protected String status_message;
protected String type;
public TABaseObject(int status_code, String status_message, String type) {
this.status_code = status_code;
this.status_message = status_message;
this.type = type;
}
}
从此对象扩展,内部对象将是:
public class TAResultObject extends TABaseObject{
@SerializedName("exito")
protected boolean exito;
@SerializedName("codigoEstado")
protected int codigoEstado;
public TAResultObject(int status_code, String status_message, boolean exito, int codigoEstado) {
super(status_code, status_message, "resultado");
this.exito = exito;
this.codigoEstado = codigoEstado;
}
@Override
public String toString() {
return super.toString() + " ////// " + "TAResultObject{" +
"exito=" + exito +
", codigoEstado=" + codigoEstado +
'}';
}
从这个其他对象,我会将TAResultObject扩展到对应的Object。
我尝试了几种反序列化方法(TypeAdapterFactory,RuntimeTypeAdapterFactory等等),没有运气。
是否有任何策略可以反序列化提到的JSON。我真的很感激这方面的任何帮助。
答案 0 :(得分:1)
这是我的Response类,
的解决方案import java.util.List;
import java.util.Map;
import com.google.gson.annotations.SerializedName;
public class TAObject {
@SerializedName("status_code")
Integer status_code ;
@SerializedName("status_message")
String status_message ;
@SerializedName("result")
Result result ;
public class Result {
@SerializedName("success")
Boolean success ;
@SerializedName("internalStatusCall")
Integer internalStatusCall ;
@SerializedName("response")
List<Map> response ;
}
}
将此类与Gson的Custom TypeAdapter一起使用。然后它将同时用于List和Object响应。
ArrayAdapter类
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class ArrayAdapter<T> extends TypeAdapter<List<T>> {
private Class<T> adapterclass;
public ArrayAdapter(Class<T> adapterclass) {
this.adapterclass = adapterclass;
}
@Override
public List<T> read(JsonReader reader) throws IOException {
List<T> list = new ArrayList<T>();
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();
final JsonToken token = reader.peek();
System.out.println(token);
// Handling of Scenario 2( Check JavaDoc for the class) :
if (token == JsonToken.STRING || token == JsonToken.NUMBER ||
token == JsonToken.BOOLEAN) {
T inning = (T) gson.fromJson(reader, adapterclass);
list.add(inning);
} else if (token == JsonToken.BEGIN_OBJECT) {
// Handling of Scenario 1(Check JavaDoc for the class) :
T inning = (T) gson.fromJson(reader, adapterclass);
list.add(inning);
} else if (token == JsonToken.BEGIN_ARRAY) {
reader.beginArray();
while (reader.hasNext()) {
@SuppressWarnings("unchecked")
T inning = (T) gson.fromJson(reader, adapterclass);
list.add(inning);
}
reader.endArray();
}
return list;
}
@Override
public void write(JsonWriter writer, List<T> value) throws IOException {
}
}
ArrayAdapterFactory类
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
public class ArrayAdapterFactory implements TypeAdapterFactory {
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type)
{
TypeAdapter<T> typeAdapter = null;
try {
if (type.getRawType() == List.class || type.getRawType() == ArrayList.class) {
typeAdapter = new ArrayAdapter(
(Class) ((ParameterizedType) type.getType())
.getActualTypeArguments()[0]);
}
} catch (Exception e) {
e.printStackTrace();
}
return typeAdapter;
}
}
并注册适配器工厂,
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();