0
 [
  {
    "sentence": "I want to buy shoes .", 
    "tree": {
      "ROOT": [
        {
          "index": 2, 
          "token": "want", 
          "label": "VERB", 
          "pos": "VBP", 
          "tree": {
            "nsubj": [
              {
                "index": 1, 
                "token": "I", 
                "label": "PRON", 
                "pos": "PRP"
              }
            ], 
            "xcomp": [
              {
                "index": 4, 
                "token": "buy", 
                "label": "VERB", 
                "pos": "VB", 
                "tree": {
                  "aux": [
                    {
                      "index": 3, 
                      "token": "to", 
                      "label": "PRT", 
                      "pos": "TO"
                    }
                  ], 
                  "dobj": [
                    {
                      "index": 5, 
                      "token": "shoes", 
                      "label": "NOUN", 
                      "pos": "NNS"
                    }
                  ]
                }
              }
            ], 
            "punct": [
              {
                "index": 6, 
                "token": ".", 
                "label": ".", 
                "pos": "."
              }
            ]
          }
        }
      ]
    }
  }
]

This is tree represented in Json. But the keys for nested nodes keep changing.
For example "ROOT, nsubj, xcomp" ... etc.
How do I convert above json code to Java Object using gson.

Above response is from syntaxnet Parsey_Mcparseface api I'm trying to use.
Thanks in advance.

amadamala
  • 323
  • 1
  • 4
  • 18
  • FYI https://stackoverflow.com/questions/1957406/generate-java-class-from-json – hiropon Sep 28 '17 at 01:40
  • Possible duplicate of [Generate Java class from JSON?](https://stackoverflow.com/questions/1957406/generate-java-class-from-json) – Carlos Laspina Sep 28 '17 at 02:33
  • Using a tool like http://www.jsonschema2pojo.org/ won't work in this case. Because it will create classes for nsubj, xcomp, aux etc to have their own classes. There can be so many of these links. My goal is to create Tree data structure from the above JSON. – amadamala Sep 28 '17 at 05:52
  • @amadamala I got it. If this sentence nodes represent binary tree, another json library jackson is helpful [Deserialize a JSON String to a Binary Tree](http://www.soulmachine.me/blog/2015/06/22/deserialize-a-json-string-to-a-binary-tree/). Is that your desired object structure ? – hiropon Sep 28 '17 at 06:57

1 Answers1

0

Gson has a method Gson#fromJson. For example, this is a code to read a simple String object.

    Gson gson = new Gson();
    String str = gson.fromJson("\"hello\"", String.class);
    System.out.println("String: " + str);

You need to prepare Java Object to read your proposed JSON. But, you don't need to write code by yourself. There is a website providing automatical JSON object generator.

jsonschema2pojo

enter following items:

  • Target language: Java
  • Source type: JSON
  • Annotation type: Gson

and enter your class name, for example "ParsedSentence"

then, write code. You will get object.

    Gson gson = new Gson();
    ParsedSentence parsed = gson.fromJson(longLongJsonString, ParsedSentence.class);

jsonschema2pojo screenshot

hiropon
  • 1,675
  • 2
  • 18
  • 41
  • I have already tried to use this route. But here is the problem.It creates classes for xcomp, aux, punct, dobj etc. There are going to be many of these keys. It's not practical to create one class for each of these. I was hoping for a solution to parse this into Java Object tree. – amadamala Sep 28 '17 at 05:38