Mohamed omar
Mohamed omar

Reputation: 57

Recursive Method to create Json object

I like to know how I might do the following:

I want to create a json format of the following:

I want to be able to create a recursive function that takes an object holding a list of other objects of the same type and in a method to recursively create the format below.

{
    "name": "lib",
    "contains": [{
        "name": "room",
        "contains": [{
            "name": "bookshelf",
            "contains": [{
                "name": "shelf",
                "contains": []
            }]
        }]
    }]
}

I have this as the following method:

private JSONObject json = new JSONObject();

public  JSONObject setupLib(Contains contain) {

    int count = contain.getContainerList().size();

    for(int i = 0; i < count; i++){
        try {

            json.put("name", contain.getContainerList().get(i).getContainerName());

            if(contain.getContainerList().size() != 0) {
                Contains contains = (Contains) contain.getContainerList().get(i);

                JSONArray array = new JSONArray();

                json.put("contain",array.put(setupLib(contains)));}
        }catch (JSONException e){
            Log.i(Tag, e.getMessage());
        }
    }

    return json;

} I get a stackoverflow on the array/object

Upvotes: 3

Views: 6798

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191831

Two options

  1. Do it yourself recursively
  2. Use a library such as Gson to save you the development time and effort

Since this is a learning experience, I have shown both that return this JSON.

{
    "name": "lib",
    "contains": [{
        "name": "room",
        "contains": [{
            "name": "bookshelf",
            "contains": [{
                "name": "shelf",
                "contains": []
            }]
        }]
    }]
}

public static void main(String[] args) {
    Contains lib = new Contains("lib");
    Contains room = new Contains("room");
    Contains bookshelf = new Contains("bookshelf");
    Contains shelf = new Contains("shelf");

    bookshelf.add(shelf);
    room.add(bookshelf);
    lib.add(room);

    // Option 1
    System.out.println(setupLib(lib).toJSONString());

    // Option 2
    Gson gson = new Gson();
    System.out.println(gson.toJson(lib));
}

private static JSONObject setupLib(Contains contain) {
    if (contain == null) return null;
    LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();

    JSONArray array = new JSONArray();
    for (Contains c : contain.getContainerList()) {
        JSONObject innerContain = setupLib(c);
        if (innerContain != null) {
            array.add(innerContain);
        }
    }
    map.put("name", contain.getName());
    map.put("contains", array);

    return new JSONObject(map);
}

This is the model object, for reference

public class Contains {
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("contains")
    @Expose
    private List<Contains> contains;

    public Contains(String name) {
        this.name = name;
        contains = new ArrayList<Contains>();
    }

    public void setName(String name) {
        this.name = name;
    }

    public void add(Contains c) {
        this.contains.add(c);
    }

    public void setContainerList(List<Contains> contains) {
        this.contains = contains;
    }

    public String getName() {
        return name;
    }

    public List<Contains> getContainerList() {
        return this.contains;
    }
}

Upvotes: 4

Christiaan Janssen
Christiaan Janssen

Reputation: 1053

I think is far easier if you'd serialize both the JSONObject and Contains classes. This way you'll be able to use the Jackson library to create the JSON file for you.

You can find more information about the Jackson library on the following GitHub page: https://github.com/FasterXML/jackson.

Upvotes: 0

Related Questions