gpt4 book ai didi

java - 如何使用 Gson 在我的 POJO 类上使用动态 json 值?

转载 作者:行者123 更新时间:2023-11-30 01:19:53 25 4
gpt4 key购买 nike

{
"localeCode": "",
"map": {
"DynamicName1": [],
"DynamicName2": [
{
"date": "2016-05-15T00:00:00",
"seqId": 1,
"status": 10
},
{
"date": "2016-05-16T00:00:00",
"seqId": 83,
"status": 10
}
],
"DynamicName3": [],
"DynamicName4": []
},
"respCode": 100,
"respMsg": "success",
"status": 1
}

如何正确映射这种json。如果您能看到,Dynamic 是一个动态名称。到目前为止,我已经这样做了:

public class MapModel {

public MapObject map;

public static class MapObject{
public java.util.Map<String, Student> queryStudent;

public static class Student{
public String date;
public String seqId;
public String status;
}
}
}

但是当运行应用程序时。我收到 NullPointerException。有人可以帮助我吗?

最佳答案

您将获得 NullPointerException正在访问 queryStudent你的MapObject在你的里面MapModel因为当您尝试反序列化您的 Json 时它没有正确填充。

所以要解决您的问题,请查看 Gson documentation在哪里可以看到:

You can serialize the collection with Gson without doing anything specific: toJson(collection) would write out the desired output.

However, deserialization with fromJson(json, Collection.class) will not work since Gson has no way of knowing how to map the input to the types. Gson requires that you provide a genericised version of collection type in fromJson(). So, you have three options:

  1. Use Gson's parser API (low-level streaming parser or the DOM parser JsonParser) to parse the array elements and then use Gson.fromJson() on each of the array elements.This is the preferred approach. Here is an example that demonstrates how to do this.

  2. Register a type adapter for Collection.class that looks at each of the array members and maps them to appropriate objects. The disadvantage of this approach is that it will screw up deserialization of other collection types in Gson.

  3. Register a type adapter for MyCollectionMemberType and use fromJson() with Collection.

由于您的 MapObject包含 java.util.Map但是你的类本身不是通用的,我认为你的情况的一个好方法是创建一个 Deserializer .

在此之前尝试清理您的类定义,提供构造函数以使反序列化器易于构建。您的 POJO 类可以是:

学生类(class)

public class Student{
public String date;
public String seqId;
public String status;

public Student(String date, String seqId, String status){
this.date = date;
this.seqId = seqId;
this.status = status;
}
}

MapObject 类

注:我换你Map定义,因为在您的 Json 中似乎每个 DynamicName 可能是多个学生(从你的问题中查看 DynamicName2),所以我使用 Map<String,List<Student>>而不是 Map<String,Student> :

public class MapObject{
public Map<String,List<Student>> queryStudent;

public MapObject(Map<String,List<Student>> value){
this.queryStudent = value;
}
}

map 模型类

public class MapModel {
public MapObject map;
}

现在创建一个 Deserializer为你的MapObject :

public class MapObjectDeserializer implements JsonDeserializer<MapObject> {
public MapObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {

Map<String,List<Student>> queryStudents = new HashMap<String,List<Student>>();
// for each DynamicElement...
for (Map.Entry<String,JsonElement> entry : json.getAsJsonObject().entrySet()) {

List<Student> students = new ArrayList<Student>();
// each dynamicElement has an Array so convert and add an student
// for each array entry
for(JsonElement elem : entry.getValue().getAsJsonArray()){
students.add(new Gson().fromJson(elem,Student.class));
}
// put the dinamic name and student on the map
queryStudents.put(entry.getKey(),students);
}
// finally create the mapObject
return new MapObject(queryStudents);
}
}

最后注册Deserializer并解析您的 Json:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(MapObject.class, new MapObjectDeserializer());
Gson gson = builder.create();
MapModel object = gson.fromJson(YourJson,MapModel.class);

免责声明:为了快速制作原型(prototype),我使用 groovy 对此进行了测试,我尝试保留 Java 语法,但我可能会忘记一些东西,无论如何我认为这可以让你正确的方向。

希望对你有帮助

关于java - 如何使用 Gson 在我的 POJO 类上使用动态 json 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37266961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com