11

I am trying to create a JSON object that looks like the below :

{"filters": {"defaultOperator":"OR", "filters":[]}}

What should i add to my JSON object to make a empty array object ??. Cant get my head around an empty array .

tried jo1.put("filters",new Object[0]);. This did not work .

I am testing an API which requires these array filled only in special cases, most of the time its empty. But dont know how to add it. I am creating the entry JSON like

JSONObject jo1 = new JSONObject();
                 jo1.put("defaultOperator", "OR");
                 jo1.put("filters","[]");

                 jo3 = new JSONObject();
                 jo3.put("filters", jo1);

                 //Toast.makeText(getApplicationContext(),""+jo3.toString() , Toast.LENGTH_LONG).show();


                 //json = new GsonBuilder().create().toJson(comment1, Map.class);
                 httppost.setEntity(new StringEntity(jo3.toString()));

1 Answer 1

27
 JSONArray jarr = new JSONArray();
 jo1.put("filters",jarr);

or all on one line:

jo1.put("filters", new JSONArray());
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect , some times the simple answers do wonders and help code. Thanks a Ton . accepted this one.

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.