Most visited

Recently visited

Added in API level 11

JsonWriter

public final class JsonWriter
extends Object implements Closeable

java.lang.Object
   ↳ android.util.JsonWriter


将JSON( RFC 4627 )编码值写入一个流,一次一个令牌。 该流包含字面值(字符串,数字,布尔值和空值)以及对象和数组的开始和结束分隔符。

Encoding JSON

To encode your data as JSON, create a new JsonWriter. Each JSON document must contain one top-level array or object. Call methods on the writer as you walk the structure's contents, nesting arrays and objects as necessary:

Example

Suppose we'd like to encode a stream of messages such as the following:
 [
   {
     "id": 912345678901,
     "text": "How do I write JSON on Android?",
     "geo": null,
     "user": {
       "name": "android_newb",
       "followers_count": 41
      }
   },
   {
     "id": 912345678902,
     "text": "@android_newb just use android.util.JsonWriter!",
     "geo": [50.454722, -104.606667],
     "user": {
       "name": "jesse",
       "followers_count": 2
     }
   }
 ]
This code encodes the above structure:
   public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException {
     JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
     writer.setIndent("  ");
     writeMessagesArray(writer, messages);
     writer.close();
   }

   public void writeMessagesArray(JsonWriter writer, List<Message> messages) throws IOException {
     writer.beginArray();
     for (Message message : messages) {
       writeMessage(writer, message);
     }
     writer.endArray();
   }

   public void writeMessage(JsonWriter writer, Message message) throws IOException {
     writer.beginObject();
     writer.name("id").value(message.getId());
     writer.name("text").value(message.getText());
     if (message.getGeo() != null) {
       writer.name("geo");
       writeDoublesArray(writer, message.getGeo());
     } else {
       writer.name("geo").nullValue();
     }
     writer.name("user");
     writeUser(writer, message.getUser());
     writer.endObject();
   }

   public void writeUser(JsonWriter writer, User user) throws IOException {
     writer.beginObject();
     writer.name("name").value(user.getName());
     writer.name("followers_count").value(user.getFollowersCount());
     writer.endObject();
   }

   public void writeDoublesArray(JsonWriter writer, List<Double> doubles) throws IOException {
     writer.beginArray();
     for (Double value : doubles) {
       writer.value(value);
     }
     writer.endArray();
   }

每个JsonWriter可用于编写单个JSON流。 这个类的实例不是线程安全的。 导致格式错误的JSON字符串的调用将失败,并显示IllegalStateException

Summary

Public constructors

JsonWriter(Writer out)

创建一个将JSON编码流写入 out的新实例。

Public methods

JsonWriter beginArray()

开始编码一个新的数组。

JsonWriter beginObject()

开始编码一个新的对象。

void close()

刷新并关闭这位作者和底层的 Writer

JsonWriter endArray()

结束编码当前数组。

JsonWriter endObject()

结束编码当前对象。

void flush()

确保将所有缓冲数据写入底层 Writer并刷新该作者。

boolean isLenient()

如果此作者放宽了语法规则,则返回true。

JsonWriter name(String name)

编码属性名称。

JsonWriter nullValue()

编码 null

void setIndent(String indent)

设置要在编码文档中为缩进的每个级别重复的缩进字符串。

void setLenient(boolean lenient)

配置此编写器以放宽其语法规则。

JsonWriter value(double value)

编码 value

JsonWriter value(Number value)

编码 value

JsonWriter value(boolean value)

编码 value

JsonWriter value(long value)

编码 value

JsonWriter value(String value)

编码 value

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

JsonWriter

Added in API level 11
JsonWriter (Writer out)

创建一个将JSON编码流写入out的新实例。 为了获得最佳性能,请确保Writer已缓冲; 必要时包装在BufferedWriter

Parameters
out Writer

Public methods

beginArray

Added in API level 11
JsonWriter beginArray ()

开始编码一个新的数组。 每次调用此方法必须与对endArray()的调用配对。

Returns
JsonWriter this writer.
Throws
IOException

beginObject

Added in API level 11
JsonWriter beginObject ()

开始编码一个新的对象。 每次调用此方法必须与对endObject()的调用进行endObject()

Returns
JsonWriter this writer.
Throws
IOException

close

Added in API level 11
void close ()

刷新并关闭此作者和底层的 Writer

Throws
IOException if the JSON document is incomplete.

endArray

Added in API level 11
JsonWriter endArray ()

结束编码当前数组。

Returns
JsonWriter this writer.
Throws
IOException

endObject

Added in API level 11
JsonWriter endObject ()

结束编码当前对象。

Returns
JsonWriter this writer.
Throws
IOException

flush

Added in API level 11
void flush ()

确保将所有缓冲数据写入底层 Writer并刷新该作者。

Throws
IOException

isLenient

Added in API level 11
boolean isLenient ()

如果此作者放宽了语法规则,则返回true。

Returns
boolean

name

Added in API level 11
JsonWriter name (String name)

编码属性名称。

Parameters
name String: the name of the forthcoming value. May not be null.
Returns
JsonWriter this writer.
Throws
IOException

nullValue

Added in API level 11
JsonWriter nullValue ()

编码 null

Returns
JsonWriter this writer.
Throws
IOException

setIndent

Added in API level 11
void setIndent (String indent)

设置要在编码文档中为缩进的每个级别重复的缩进字符串。 如果indent.isEmpty()编码的文件将是紧凑的。 否则,编码后的文档将会更加人性化。

Parameters
indent String: a string containing only whitespace.

setLenient

Added in API level 11
void setLenient (boolean lenient)

配置此编写器以放宽其语法规则。 默认情况下,该作者只发出RFC 4627指定的格式良好的JSON。 将作者设置为宽松允许以下内容:

  • Top-level values of any type. With strict writing, the top-level value must be an object or an array.
  • Numbers may be NaNs or infinities.

Parameters
lenient boolean

value

Added in API level 11
JsonWriter value (double value)

编码 value

Parameters
value double: a finite value. May not be NaNs or infinities unless this writer is lenient.
Returns
JsonWriter this writer.
Throws
IOException

value

Added in API level 11
JsonWriter value (Number value)

编码 value

Parameters
value Number: a finite value. May not be NaNs or infinities unless this writer is lenient.
Returns
JsonWriter this writer.
Throws
IOException

value

Added in API level 11
JsonWriter value (boolean value)

编码 value

Parameters
value boolean
Returns
JsonWriter this writer.
Throws
IOException

value

Added in API level 11
JsonWriter value (long value)

编码 value

Parameters
value long
Returns
JsonWriter this writer.
Throws
IOException

value

Added in API level 11
JsonWriter value (String value)

编码 value

Parameters
value String: the literal string value, or null to encode a null literal.
Returns
JsonWriter this writer.
Throws
IOException

Hooray!