2016-09-06 6 views
0

JSONをPOJOのインスタンスにデシリアライズする際のヘルプを探しています。最上位のPOJO Graph.javaには、HashMap型の属性があります。シリアライズながらそれはGsonを使用してJSONをデシリアライズする

期待BEGIN_ARRAYしかしBEGIN_OBJECTが、私はそれが意味を正確に知っているし、それを修正する方法[0]

ラインNNNパス $ .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(); 
    } 
} 

答えて

1

問題は、あなたが投稿JSONが有効でないということです。

Mapは任意のオブジェクトを任意のオブジェクトにマップするために使用できるため、Gsonは2つのオブジェクトを持つ配列としてマップを作成する必要があります。

マップオブジェクトの有効なJSONは次のようになります:

"degreesCountMap": [ 
    [ 
    "ONE", 
    2 
    ], 
    [ 
    "TWO", 
    1 
    ] 
] 

いますが、キーとして列挙型を使用しているので、次のコードでも有効です。

"degreesCountMap": { 
    "TWO": 1, 
    "ONE": 2 
} 

はソリューション:編集あなたjsonを有効にします。また、TWOがあなたの列挙型で見つからないと思います。

注:あなたが列挙型を使用しているため"ONE"はそこですが、あなたはそれができたキーのための典型的なオブジェクトを使用する場合は、このようになります:{ 「TWO」:

"degreesCountMap": [ 
    [ 
    { "degree": "ONE" }, 
    2 
    ], 
    [ 
    { "degree": "TWO" }, 
    1 
    ] 
] 
+0

は、 「degreesCountMapを」ありがとう: 1、 "ONE":2 }うまくいきました –

+0

あなたが役に立ったら、この答えを受け入れてください。未解決のものと見なされず、私の仕事を尊重してください。ありがとうございました。 – pr0gramist

関連する問題