0
4

1 回答 1

1

根据源代码

public String toString() {
    try {
        return this.toString(0);
    } catch (Exception e) {
        return null;
    }
}

public String toString(int indentFactor) throws JSONException {
    StringWriter w = new StringWriter();
    synchronized (w.getBuffer()) {
        return this.write(w, indentFactor, 0).toString();
    }
}

public Writer write(Writer writer) throws JSONException {
    return this.write(writer, 0, 0);
}

所以基本上,第一种方法:

myWriter.write(myJSONObj.toString());
  1. 创建一个StringWriter.
  2. 传作家到write(Writer writer, int indentFactor, int indent)
  3. JSON 内容被写入编写器。
  4. writer 的内容通过StringWriter#toString().
  5. 最后的字符串被写入myWriter.

第二种方法:

myJSONObj.write(myWriter);
  1. 传作家到write(Writer writer, int indentFactor, int indent)
  2. JSON 内容被写入编写器。
于 2013-10-09T19:46:43.317 回答