如何解析JSON数组与数组的数组(How to parse a json array with an

2019-10-30 04:41发布

我想我对这个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>  

在com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:64)在com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:92)在com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:117 )在com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)在com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)在com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java: 76)在com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:54)在com.google.gson.Gson.fromJson(Gson.java:551)在com.google.gson.Gson.fromJson(Gson.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的1:389)在com.skyscraper.transformer.Node Transformer.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个

如此反复,问题是如何正确解析此JSON输出。 这是一个从Neo4j的REST调用,如果这是任何价值。

当前实现:

   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"}}
        ]
    ]
}

Answer 1:

尝试使用这样的:

private List<List<Object>> f = new ArrayList<List<Object>>();

然后f.get(0).get(0)会给你的“类型”,和一个String f.get(0).get(1)会给你JsonObject

http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonObject.html

另外,返回你需要什么了在单独的列的关系的数据,而不是整个关系图。

更新:也许你应该只使用springdata,Neo4j的。 :)



文章来源: How to parse a json array with an array of arrays