このjsonオブジェクトの解析に関して、ここで何かが欠けていると思います。受け取る出力は次の形式です。
{
"columns" : [ "type", "relation" ],
"data" : [ [ "SOURCE", {
"paged_traverse" : "localhost/db/data/node/26/paged/traverse/{returnType}{?pageSize,leaseTime}",
"outgoing_relationships" : "localhost/db/data/node/26/relationships/out",
"data" : {
"DATUM" : "December"
},
"all_typed_relationships" : "localhost/db/data/node/26/relationships/all/{-list|&|types}",
"traverse" : "localhost/db/data/node/26/traverse/{returnType}",
"self" : "localhost/db/data/node/26",
"all_relationships" : "localhost/db/data/node/26/relationships/all",
"property" : "localhost/db/data/node/26/properties/{key}",
"outgoing_typed_relationships" : "localhost/db/data/node/26/relationships/out/{-list|&|types}",
"properties" : "localhost/db/data/node/26/properties",
"incoming_relationships" : "localhost/db/data/node/26/relationships/in",
"incoming_typed_relationships" : "localhost/db/data/node/26/relationships/in/{-list|&|types}",
"extensions" : {
},
"create_relationship" : "localhost/db/data/node/26/relationships"
} ]]
}
GSON 内で最も内側の部分を正しく解析できました。
public class Data
{
private String paged;
private String out;
private Map<String, String> data;
...
}
しかし、全身を提供すると、次の例外が発生します。
Exception in thread "main" com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialize json object
{
"columns" : [ "type", "relation" ],
"data" : [ [ "SOURCE", {
"paged" : foo/{returnType}{?pageSize,leaseTime}",
"out" : "bar",
"data" : {
"DATUM" : "December"
}
} ],
[ "SOURCE", {
"paged" : "test/{returnType}{?pageSize,leaseTime}",
"out" : "baz",
"data" : {
"DATUM" : "Steve"
}
}
]
]
} given the type java.util.Map<java.lang.String, java.lang.String>
java:498) com.google.gson.Gson.fromJson(Gson.java:467) com.google.gson.Gson.fromJson(Gson.java:417) com.google.gson.Gson.fromJson(Gson) .java:389) で com.skyscraper.transformer.NodeTransformer.fromGson(NodeTransformer.java:24) で com.skyscraper.query.read.Retriever.queryNode(Retriever.java:128) で com.skyscraper.query.read. Retriever.main(Retriever.java:67) 原因: java.lang.IllegalStateException: これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) ... 14 もっと見る 467) com.google.gson.Gson.fromJson(Gson.java:417) com.google.gson.Gson.fromJson(Gson.java:389) com.skyscraper.transformer.NodeTransformer.fromGson(NodeTransformer.java) :24) com.skyscraper.query.read.Retriever.queryNode(Retriever.java:128) で com.skyscraper.query.read.Retriever.main(Retriever.java:67) 原因: java.lang.IllegalStateException:これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) ... 14 もっと見る 467) com.google.gson.Gson.fromJson(Gson.java:417) com.google.gson.Gson.fromJson(Gson.java:389) com.skyscraper.transformer.NodeTransformer.fromGson(NodeTransformer.java) :24) com.skyscraper.query.read.Retriever.queryNode(Retriever.java:128) で com.skyscraper.query.read.Retriever.main(Retriever.java:67) 原因: java.lang.IllegalStateException:これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) で ... 14 もっと見る 389) com.skyscraper.transformer.NodeTransformer.fromGson(NodeTransformer.java:24) で com.skyscraper.query.read.Retriever.queryNode(Retriever.java:128) で com.skyscraper.query.read.Retriever.main (Retriever.java:67) 原因: java.lang.IllegalStateException: これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) で ... 14 もっと見る 389) com.skyscraper.transformer.NodeTransformer.fromGson(NodeTransformer.java:24) で com.skyscraper.query.read.Retriever.queryNode(Retriever.java:128) で com.skyscraper.query.read.Retriever.main (Retriever.java:67) 原因: java.lang.IllegalStateException: これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) で ... 14 もっと見る java:67) 原因: java.lang.IllegalStateException: これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) で ... 14 もっと見る java:67) 原因: java.lang.IllegalStateException: これは JSON オブジェクトではありません。com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:83) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:66) com.google.gson.MapTypeAdapter.deserialize(MapTypeAdapter.java:33) ) com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:51) で ... 14 もっと見る
繰り返しますが、問題は、この json 出力を適切に解析するにはどうすればよいかということです。それが何らかの価値がある場合、それはneo4jレストコールからのものです。
現在の実装:
import java.util.ArrayList;
import java.util.List;
public class Wrapper {
private String[] columns = new String[] { "type", "relation" };
private List<List<Object>> f = new ArrayList<List<Object>>();
public String[] getColumns() {
return columns;
}
public void setColumns(String[] columns) {
this.columns = columns;
}
public List<List<Object>> getF() {
return f;
}
public void setF(List<List<Object>> f) {
this.f = f;
}
}
public class NodeTransformer {
public static void main(String[] a) {
Gson gson = new Gson();
Wrapper w = new Wrapper();
List<List<Object>> blah = w.getF();
List<Object> objects = new ArrayList<Object>();
objects.add(new DataNode());
objects.add(new DataNode());
blah.add(objects);
w.setF(blah);
System.out.println(gson.toJson(w));
}
収量:
{
"columns":["type","relation"],
"f": [ [
{"paged_traverse":"localhost/db/data/node/25/paged/traverse/{returnType}{?pageSize,leaseTime}",
"outgoing_relationships":"localhost/db/data/node/25/relationships/out",
"data":{},
"all_typed_relationships":"localhost/db/data/node/25/relationships/all/{-list|\u0026|types}",
"traverse":"localhost/db/data/node/25/traverse/{returnType}",
"self":"localhost/db/data/node/25",
"all_relationships":"localhost/db/data/node/25/relationships/all",
"property":"localhost/db/data/node/25/properties/{key}",
"outgoing_typed_relationships":"localhost/db/data/node/25/relationships/out/{-list|\u0026|types}",
"properties":"localhost/db/data/node/25/properties",
"incoming_relationships":"localhost/db/data/node/25/relationships/in",
"incoming_typed_relationships":"localhost/db/data/node/25/relationships/in/{-list|\u0026|types}",
"extensions":{},
"create_relationship":"localhost/db/data/node/25/relationships"}}
]
]
}