1

Hello I am stuck in converting JSON NESTED Array to JAVA Objects using GSON labirary.

Here is my JSON String

[{\"name\":\"3214657890RootSAPSSE\",\"children\":[{\"name\":\"672BENIAMEEN .Sales Base Market SectionCustomer Representative\"},{\"name\":\"672BENIAMEEN .Sales Base Market SectionPeão\"}]}] 

I tested my code with x="[{\"name\":\"MyNode\"}]"; and for this my java code is

Gson gson = new Gson(); 
java.lang.reflect.Type type = new TypeToken<List<EmployeeJSONObj>>(){}.getType();
List<EmployeeJSONObj>l = gson.fromJson(x, type);
System.out.println("hello "+l.get(0).getName());

AND EmployeeJSONObj is class

public class EmployeeJSONObj {
    private String name;
    EmployeeJSONObj()
    {

    }
    public String getName()
    {
        return name;
    }

}

This is working fine. But my this code gives me error when I change the JSON String to

[{\"name\":\"3214657890RootSAPSSE\",\"children\":[{\"name\":\"672BENIAMEEN .Sales Base Market SectionCustomer Representative\"},{\"name\":\"672BENIAMEEN .Sales Base Market SectionPeão\"}]}] 

This is a tree. I want generic solution, which can traverse the whole nested json string. I'm stuck in the middle of a project. Please help me to solve this.

Thanks

1
  • Does it always have children? Commented Sep 20, 2013 at 12:47

1 Answer 1

1

You need a List of children in your class so that Gson can deserialize the children array. Here's a working example.

public static void main(String args[]) {
    String str = "[{\"name\":\"3214657890RootSAPSSE\",\"children\":[{\"name\":\"672BENIAMEEN .Sales Base Market SectionCustomer Representative\"},{\"name\":\"672BENIAMEEN .Sales Base Market SectionPeão\"}]}]";
    System.out.println(str);

    Gson gson = new Gson(); 
    java.lang.reflect.Type type = new TypeToken<List<EmployeeJSONObj>>(){}.getType();
    List<EmployeeJSONObj>l = gson.fromJson(str, type);
    System.out.println(l);
}
public static  class EmployeeJSONObj {
    private String name;
    private List<EmployeeJSONObj> children = new LinkedList<>();
    EmployeeJSONObj()
    {

    }
    public String getName()
    {
        return name;
    }

    public String toString() {
        return "name: " + name + ", children = " + children;
    }

}

prints

[{"name":"3214657890RootSAPSSE","children":[{"name":"672BENIAMEEN .Sales Base Market SectionCustomer Representative"},{"name":"672BENIAMEEN .Sales Base Market SectionPeão"}]}]
[name: 3214657890RootSAPSSE, children = [name: 672BENIAMEEN .Sales Base Market SectionCustomer Representative, children = [], name: 672BENIAMEEN .Sales Base Market SectionPeão, children = []]]
Sign up to request clarification or add additional context in comments.

15 Comments

I think thi is not working for nested json - I mean My json is generating dynamically. So user can make as many child as she wants...
@user2777070 Provide an example that you think won't work. Each EmployeeJSONObj has its own children. It'll nest as many as it needs to.
When I give it dynamic JSON it gives me this error "Expected BEGIN_ARRAY but was STRING at line 1 column 2"
@user2777070 You're trying a different format of JSON than in your example. You're giving it an object instead of an array.
How do I get the stored objects in java from this code? istead of showing whole json string?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.