JSONをPOJOのインスタンスにデシリアライズする際のヘルプを探しています。最上位のPOJO Graph.javaには、HashMap型の属性があります。シリアライズながらそれはGsonを使用してJSONをデシリアライズする
期待BEGIN_ARRAYしかしBEGIN_OBJECTが、私はそれが意味を正確に知っているし、それを修正する方法[0]
ラインN列NNパス $ .degreesCountMapにあったがスローされますトップレベルのコレクションでは、他のオブジェクトの属性のTypeを指定する方法が不明です。
私は、この問題や他の多くのフォーラムでこのような問題に関する議論を検討しましたが、私は本当に役立つ回答はありません。
私はこの点について大きな助けになります。
はここでグラフのJSONです:ここで
{
"nodeCount":3,
"edgeCount":2,
"degreesCountMap":[
{
"ONE":2
},
{
"TWO":1
}
],
"nodes":[
{
"index":0,
"connectedIndices":[
1
]
},
{
"index":1,
"connectedIndices":[
0,
2
]
},
{
"index":2,
"connectedIndices":[
1
]
}
]
}
はPOJOである
Graph.java
public class Graph {
private HashMap<Degree, Integer> degreesCountMap;
private Integer edgeCount;
private Integer nodeCount;
private ArrayList<Node> nodes;
public HashMap<Degree, Integer> getDegreesCountMap() {
return degreesCountMap;
}
public void setDegreesCountMap(HashMap<Degree, Integer> degreesCountMap) {
this.degreesCountMap = degreesCountMap;
}
public void setNodes(ArrayList<Node> nodes) {
this.nodes = nodes;
}
}
Degree.java
public enum Degree {
ZERO, ONE, THREE, FOUR;
}
Node.java
public class Node {
private ArrayList<Integer> connectedIndices;
private int index;
public ArrayList<Integer> getConnectedIndices() {
return connectedIndices;
}
public int getIndex() {
return index;
}
public void setConnectedIndices(ArrayList<Integer> connectedIndices) {
this.connectedIndices = connectedIndices;
}
public void setIndex(int index) {
this.index = index;
}
}
GraphTest.java
@Test
public void testJsonToGraph() {
String json = "{\"nodeCount\":3,\"edgeCount\":2,"
+ "\"degreesCountMap\":[{\"ONE\":2},{\"TWO\":1}],"// <--to fail
+ "\"nodes\":[{\"index\":0,\"connectedIndices\":[1]},"
+ "{\"index\":1,\"connectedIndices\":[0,2]},"
+ "{\"index\":2,\"connectedIndices\":[1]}]}";
try {
graph = gson.fromJson(json, Graph.class);
assertNotNull(graph);
} catch (Exception e) { // Intentionally capturing to diagnose
e.printStackTrace();
}
}
は、 「degreesCountMapを」ありがとう: 1、 "ONE":2 }うまくいきました –
あなたが役に立ったら、この答えを受け入れてください。未解決のものと見なされず、私の仕事を尊重してください。ありがとうございました。 – pr0gramist