java 将空数组添加到 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30904675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding empty array to a JSON object
提问by anil keshav
I am trying to create a JSON object that looks like the below :
我正在尝试创建一个如下所示的 JSON 对象:
{"filters":
{"defaultOperator":"OR",
"filters":[]}}
{"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 .
我应该向我的 JSON 对象添加什么来创建一个空数组对象??。无法理解一个空数组。
tried jo1.put("filters",new Object[0]);
. This did not work .
试过了jo1.put("filters",new Object[0]);
。这没有用。
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
我正在测试一个 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()));
回答by
JSONArray jarr = new JSONArray();
jo1.put("filters",jarr);
or all on one line:
或全部在一行上:
jo1.put("filters", new JSONArray());