gpt4 book ai didi

java - 使用 GSON 将两个 ArrayList 转换为 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:42 25 4
gpt4 key购买 nike

我有一个类 Graph,其中有两个 ArrayList 作为成员变量,我想使用 GSON 库将其转换为 JSON 文件。

这是 Graph 类的代码:

public class Graph {

private List<Node> nodes;
private List<Link> links;

// Constructors, Getters and Setters


}

这是 Node 类的代码:

public class Node {

private String name;
private int group;

// Constructors, Getters and Setters


}

这是类链接的代码:

public class Link {

private int source;
private int target;

// Constructors, Getters and Setters


}

这是我想要生成的 JSON 文件:

{
"nodes":[
{"name":"Myriel","group":1},
{"name":"Napoleon","group":1},
{"name":"Mlle.Baptistine","group":2},
{"name":"Mme.Hucheloup","group":2}
],
"links":[
{"source":1,"target":0},
{"source":2,"target":0},
{"source":3,"target":1},
{"source":3,"target":2}
]
}

这是主类:

public class Main {

public static void main(String[] args) throws IOException {

Graph graph = new Graph();

List<Node> nodes = new ArrayList<Node>();
nodes = DAO.listNodes(); //This function fills the list from a database

List<Link> links = new ArrayList<Link>();
links = DAO.listLinks(); //This function fills the list from a database

graph.setNodes(nodes);
graph.setLinks(links);

Writer writer = new FileWriter("output.json");

Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(graph, writer);

writer.close();

}

}

这是我得到的错误:

Exception in thread "main" java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:68)
at com.google.gson.internal.bind.TypeAdapters$1.write(TypeAdapters.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ArrayTypeAdapter.write(ArrayTypeAdapter.java:93)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:206)
at com.google.gson.Gson.toJson(Gson.java:595)
at com.google.gson.Gson.toJson(Gson.java:574)
at com.google.gson.Gson.toJson(Gson.java:549)
at gson.Main.main(Main.java:36)

有什么问题吗?我该如何解决它?

最佳答案

我找到了解决方案:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Node.class, new NodeAdapter()).registerTypeAdapter(Link.class, new LinkAdapter());
Gson gson = gsonBuilder.create();
writer.write(gson.toJson(graph));

它确实按照我想要的方式生成了一个 JSON 文件。

感谢您的帮助!

这是 LinkAdapter 类:

public class LinkAdapter implements JsonSerializer<Link> {

public JsonElement serialize(Link link, Type type,
JsonSerializationContext jsc) {

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("source", link.getNodeBySource().getIdNode());
jsonObject.addProperty("target", link.getNodeByTarget().getIdNode());

return jsonObject;
}

}

这是 NodeAdapter 类:

public class NodeAdapter implements JsonSerializer<Node> {

public JsonElement serialize(Node node, Type type,
JsonSerializationContext jsc) {

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", node.getName());
jsonObject.addProperty("group", node.getHyperEdge().getIdHe());

return jsonObject;
}


}

关于java - 使用 GSON 将两个 ArrayList 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484363/

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