Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 520
Mohamed omar Avatar asked Feb 02 '26 16:02

Mohamed omar


1 Answers

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;
    }
}
like image 109
OneCricketeer Avatar answered Feb 05 '26 05:02

OneCricketeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!