如何使用数组数组解析json数组

如何使用数组数组解析json数组

本文介绍了如何使用数组数组解析json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我在解析此json对象时在这里丢失了一些东西.我收到的输出格式如下:

I think I am missing something here on the parsing of this json object. The output I receive is in this format:

   {
  "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中正确解析了最内层部分:

I have managed to parse the inner-most part correctly within GSON:

public class Data
{
    private String paged;
    private String out;
    private Map<String, String> data;
        ...
}

但是当我提供全身时,会出现以下异常:

But when I supply the full body I get the following exception:

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>

因此,问题再次出在我该如何正确解析此json输出.如果它具有任何值,则来自neo4j rest调用.

So again the question is how do I properly parse this json output. It is from a neo4j rest call if that is of any value.

当前实施:

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

推荐答案

尝试使用此功能:

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

然后f.get(0).get(0)将给您一个类型"的字符串,而f.get(0).get(1)将给您一个JsonObject

And then f.get(0).get(0) will give you a String of "type", and f.get(0).get(1) will give you a JsonObject

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

或者,从关系数据中的单独列中而不是整个关系图中返回所需的内容.

Alternatively, return what you need out of the relationship data in separate columns, rather than the whole relationship map.

更新:也许您应该只使用springdata-neo4j. :)

Update: Maybe you should just be using springdata-neo4j. :)

这篇关于如何使用数组数组解析json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:07