Arrays 在json对象中累积json数组时添加了额外的方括号
请澄清为什么在json对象中存储json数组时会得到额外的方括号 使用的json版本:json-20140107.jarArrays 在json对象中累积json数组时添加了额外的方括号,arrays,json,jsonobject,Arrays,Json,Jsonobject,请澄清为什么在json对象中存储json数组时会得到额外的方括号 使用的json版本:json-20140107.jar JSONObject jsonObj = new JSONObject(); JSONArray jsonArray = new JSONArray(); jsonArray.put("value"); jsonObj.accumulate("A", jsonArray); System.out.println(" jsonObj "
JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonArray.put("value");
jsonObj.accumulate("A", jsonArray);
System.out.println(" jsonObj " + jsonObj);
输出:
jsonObj {"A":[["value"]]}
旧行为:
jsonObj {"A":["value"]}
因为方法:
我知道。但在旧版本中,它被恰当地处理了。我在这里提出这个问题是为了问这个@marvin背后的原因
public JSONObject accumulate(String key, Object value) throws JSONException {
testValidity(value);
Object object = this.opt(key);
if (object == null) {
this.put(key,
value instanceof JSONArray ? new JSONArray().put(value)
: value);
} else if (object instanceof JSONArray) {
((JSONArray) object).put(value);
} else {
this.put(key, new JSONArray().put(object).put(value));
}
return this;
}