Java Gson将JSON数组反序列化为对象
我有一个对象,需要从JSON(使用Gson)反序列化/反编组。我的问题是,在我的JavaBean中,其中一个属性是一个复杂的对象,但需要从一个JSON数字数组中序列化: JSON是(有点类似于): 目标类包括(与之类似): 我的GsonBuilder配置为:Java Gson将JSON数组反序列化为对象,java,android,json,gson,deserialization,Java,Android,Json,Gson,Deserialization,我有一个对象,需要从JSON(使用Gson)反序列化/反编组。我的问题是,在我的JavaBean中,其中一个属性是一个复杂的对象,但需要从一个JSON数字数组中序列化: JSON是(有点类似于): 目标类包括(与之类似): 我的GsonBuilder配置为: new GsonBuilder() .registerTypeAdapter(Location.class, new LocationTypeConverter()) .create(); 我的LocationTypeCo
new GsonBuilder()
.registerTypeAdapter(Location.class, new LocationTypeConverter())
.create();
我的LocationTypeConverter如下所示:
public class LocationTypeConverter extends TypeAdapter<Location> implements JsonSerializer<Location>, JsonDeserializer<Location> {
private static final int LONGITUDE_INDEX = 0;
private static final int LATITUDE_INDEX = 1;
@Override
public JsonElement serialize(Location src, Type srcType, JsonSerializationContext context) {
JsonArray array = new JsonArray();
array.set(LATITUDE_INDEX, new JsonPrimitive(src.getLatitude()));
array.set(LONGITUDE_INDEX, new JsonPrimitive(src.getLongitude()));
return array;
}
@Override
public Location deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
return new Location(array.get(LATITUDE_INDEX).getAsDouble(), array.get(LONGITUDE_INDEX).getAsDouble());
}
@Override
public void write(JsonWriter out, Location value) throws IOException {
out.beginArray().value(value.getLongitude()).value(value.getLatitude()).endArray();
}
@Override
public Location read(JsonReader in) throws IOException {
in.beginArray();
double longitude = in.nextDouble();
double latitude = in.nextDouble();
in.endArray();
return new Location(longitude, latitude);
}
}
{
"name": "John Doe",
"location": {
"longitude": 9.560006,
"latitude": 55.719474
}
}
需要反序列化的两个类(包含Location实例的类和“包装”对象本身)都是简单的JavaBean(使用适当的set'er和get'er方法)
没有调用任何序列化、反序列化或读取的方法,那么我在这里做错了什么
提前感谢您的帮助
编辑#1:添加了目标类/bean,以澄清我试图反序列化到的对象图。这是您需要对编写的JSON进行服务/编码的类:
MyObject {
public String name;
public float[] location;
public float getLatitude() {
if (location != null) return location[0];
return 0;
}
public float getLongitude() {
if (location != null) return location[1];
return 0;
}
}
而您编写的类将具有如下JSON:
public class LocationTypeConverter extends TypeAdapter<Location> implements JsonSerializer<Location>, JsonDeserializer<Location> {
private static final int LONGITUDE_INDEX = 0;
private static final int LATITUDE_INDEX = 1;
@Override
public JsonElement serialize(Location src, Type srcType, JsonSerializationContext context) {
JsonArray array = new JsonArray();
array.set(LATITUDE_INDEX, new JsonPrimitive(src.getLatitude()));
array.set(LONGITUDE_INDEX, new JsonPrimitive(src.getLongitude()));
return array;
}
@Override
public Location deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
return new Location(array.get(LATITUDE_INDEX).getAsDouble(), array.get(LONGITUDE_INDEX).getAsDouble());
}
@Override
public void write(JsonWriter out, Location value) throws IOException {
out.beginArray().value(value.getLongitude()).value(value.getLatitude()).endArray();
}
@Override
public Location read(JsonReader in) throws IOException {
in.beginArray();
double longitude = in.nextDouble();
double latitude = in.nextDouble();
in.endArray();
return new Location(longitude, latitude);
}
}
{
"name": "John Doe",
"location": {
"longitude": 9.560006,
"latitude": 55.719474
}
}
最后,这是我对您的代码所做的测试,它运行良好!可能您只需要确保始终使用相同的Gson对象(从GsonBuilder.create获得的对象)
我认为你不需要一个特殊的
registerTypeAdapter
。只需创建一个位置。class
带有字符串和列表
可能重复的@jackk,我怀疑它是重复的-你指的问题是整个响应是一个数组,而Gson反序列化机制是针对一个对象的。我的问题是关于解析一个对象的单个属性。对不起,我忘了发布我的“目标”对象图…我已经有了它(所以我发布了一个更新)…谢谢你的反馈。对不起,但我真的想要一个(复杂的)对象,而不是浮点数组,因为我的应用程序中的多个对象取决于我的Location类的行为。Location类还与方法等有距离(仅遵循sound OOP原则)。
{
"name": "John Doe",
"location": {
"longitude": 9.560006,
"latitude": 55.719474
}
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(Location.class, new LocationTypeConverter())
.setPrettyPrinting()
.create();
UserLocation userLocation = new UserLocation();
userLocation.name = "ciao";
userLocation.location = new Location(1.0, 3.0);
String json = gson.toJson(userLocation);
System.out.println(json);
UserLocation newlocation = gson.fromJson(json, UserLocation.class);
System.out.print(newlocation.location.getLatitude());