[英]Convert Two ArrayLists to JSON using GSON
I have a Class Graph
having two ArrayLists as member variables and I would like to convert it into a JSON file using the GSON library. 我有一个包含两个ArrayLists作为成员变量的Class Graph
,我想使用GSON库将其转换为JSON文件。
This is the code of the class Graph : 这是Graph类的代码:
public class Graph {
private List<Node> nodes;
private List<Link> links;
// Constructors, Getters and Setters
}
This is the code of the class Node : 这是Node类的代码:
public class Node {
private String name;
private int group;
// Constructors, Getters and Setters
}
This is the code of the class Link : 这是Link类的代码:
public class Link {
private int source;
private int target;
// Constructors, Getters and Setters
}
This is the JSON file I would like to generate : 这是我要生成的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}
]
}
This is the Main class : 这是主类:
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();
}
}
And this is the error I get : 这是我得到的错误:
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)
What is the problem? 问题是什么? And how can I fix it? 我该如何解决?
I found a solution : 我找到了解决方案:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Node.class, new NodeAdapter()).registerTypeAdapter(Link.class, new LinkAdapter());
Gson gson = gsonBuilder.create();
writer.write(gson.toJson(graph));
It did generate a JSON file the way I wanted it. 它确实按照我想要的方式生成了一个JSON文件。
Thanks for your help! 谢谢你的帮助!
This is the LinkAdapter
class : 这是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;
}
}
And this is the NodeAdapter
class : 这是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;
}
}
Do this to pull the elements out of the proxy lists into the ArrayLists: 这样做可以将元素从代理列表中拉出到ArrayLists中:
List<Node> nodes = new ArrayList<Node>();
nodes.addAll(DAO.listNodes());
List<Link> links = new ArrayList<Link>();
links.addAll(DAO.listLinks());
First get JSON string from gson and then write it into file. 首先从gson获取JSON字符串,然后将其写入文件。
Graph graph = new Graph()
//This function fills the list from a database
List<Node> nodes = DAO.listNodes();
//This function fills the list from a database
List<Link> links = DAO.listLinks();
graph.setNodes(nodes);
graph.setLinks(links);
File file = new File("output.json");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Write JSON content to the file
writer.write(gson.toJson(graph));
writer.flush();
writer.close();
Thank you 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.