将空数组添加到JSON对象

时间:2015-06-18 00:30:24

标签: java arrays json

我正在尝试创建一个如下所示的JSON对象:

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

我应该将什么添加到我的JSON对象中以创建一个空的数组对象?无法绕过一个空阵列。

尝试jo1.put("filters",new Object[0]);。这没用。

我正在测试一个API,它只需要在特殊情况下填充这些数组,大部分时间都是空的。但不知道如何添加它。我正在创建像

这样的条目JSON
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 个答案:

答案 0 :(得分:13)

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

或全部在一行:

jo1.put("filters", new JSONArray());
相关问题