我正在尝试创建一个如下所示的JSON对象:
{"filters":
{"defaultOperator":"OR",
"filters":[]}}
我应该将什么添加到我的JSON对象中以创建一个空的数组对象?无法绕过一个空阵列。
尝试jo1.put("filters",new Object[0]);
。这没用。
我正在测试一个API,它只需要在特殊情况下填充这些数组,大部分时间都是空的。但不知道如何添加它。我正在创建像
这样的条目JSONJSONObject 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()));
答案 0 :(得分:13)
JSONArray jarr = new JSONArray();
jo1.put("filters",jarr);
或全部在一行:
jo1.put("filters", new JSONArray());