0

I have a JSON file which have separate subsections and all the subsections have same type of data, is there any way we can directly map these inner objects

{
  "pre-execution": {
    "nodes": [],
    "links": []
  },
  "execution": {
    "nodes": [],
    "links": []
  },
  "completion": {
    "nodes": [],
    "links": []
  }
}

I am interested only in node and link type of data and not bothered about from which category it belongs to.

My class structure looks like this

public class NodeElement{
  private List<Node> nodes;
  private List<Link> links;
}

Is there any way i can directly map it through Gson library. The code i am using is giving null

   Gson gson=new Gson();
   NodeElement nodeElements=gson.fromJson(jsonString,NodeElement.class);

the reason i got because it is not following the correct hierarchy. The point is I am interested in only Node and Link so why should worry for the hierarchy.

Any other library can be used if not Gson?

i tried doing this way

for(Map.Entry<String,JsonElement> entry : entrySet){
        if(entry.getKey().contains("node")){
            siteFlowElement.addNodes((List<GoJSNode>) obj.get(entry.getKey()));
        }
        if(entry.getKey().contains("link")){
            siteFlowElement.addLinks((List<GoJSLink>) obj.get(entry.getKey()));
        }
    }

    System.out.println("Nodes"+siteFlowElement.getNodes());
    System.out.println("Links"+siteFlowElement.getLinks());

But it is returning null,

Json object is populating fine

Ravi
  • 719
  • 8
  • 23
  • You can take a look @ https://stackoverflow.com/questions/19551242/parsing-a-complex-json-object-using-gson-in-java This might help – Rahul Jun 21 '17 at 08:54
  • it is not helpful , as the question is for parsing json through Gson , i know that already the problem is that i don't want to make exact hierarchy of class as i am interested in only node and links – Ravi Jun 21 '17 at 08:58

1 Answers1

1
JsonParser parser = new JsonParser();
JsonObject o = parser.parse(your json string).getAsJsonObject();

After getting the JsonObject Iterate through the object and get all the nodes and link inside of a list for all the keys

eg - Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
  • i did something like this ~ Set> entrySet = obj.entrySet(); for(Map.Entry entry : entrySet){ if(entry.getKey().contains("node")){ siteFlowElement.addNodes((List) obj.get(entry.getKey())); } if(entry.getKey().contains("link")){ siteFlowElement.addLinks((List) obj.get(entry.getKey())); } } – Ravi Jun 21 '17 at 11:59
  • it is giving null values in node and link...Can u tell the problem in code – Ravi Jun 21 '17 at 12:00
  • what is the error can you update it in the question its hard to figure out it out in the comment section – Rahul Singh Jun 21 '17 at 12:00
  • is the jsonObject getting populated fine ? did you try and print that – Rahul Singh Jun 21 '17 at 12:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147262/discussion-between-ravi-and-rahul-singh). – Ravi Jun 21 '17 at 12:05