Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在json对象中累积json数组时添加了额外的方括号_Arrays_Json_Jsonobject - Fatal编程技术网

Arrays 在json对象中累积json数组时添加了额外的方括号

Arrays 在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 "

请澄清为什么在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 " + 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;
}