Android JSON写入类 - JsonWriter

在Android 3.0中新增了一个JSON写入类android.util.JsonWriter,使用JsonWriter可以轻松的生成JSON格式的数据,比如下面的JSON数据为

[
{
“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
}
}
]}

上面的JSON数据在Android 3.0 honeycomb上的写入代码为

public void writeJsonStream(OutputStream out, List messages) throws IOException {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, “UTF-8”)); //android开发网提示这是UTF-8编码
writer.setIndent(" ");
writeMessagesArray(writer, messages);
writer.close();

public void writeMessagesArray(JsonWriter writer, List 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 doubles) throws IOException {
writer.beginArray();
for (Double value : doubles) {
writer.value(value);
}
writer.endArray();
}}

这里我们可以看到Android 3.0开始的JsonWriter类可以很轻松的写入json格式的数据流,整个类的成员有

Public Constructors
JsonWriter(Writer out) //公共构造方法,参数为Writer类这是一个I/O类的对象

Public Methods
JsonWriter beginArray()
Begins encoding a new array.
JsonWriter beginObject()
Begins encoding a new object.
void close()
Flushes and closes this writer and the underlying Writer.
JsonWriter endArray()
Ends encoding the current array.
JsonWriter endObject()
Ends encoding the current object.
void flush()
Ensures all buffered data is written to the underlying Writer and flushes that writer.
boolean isLenient()
Returns true if this writer has relaxed syntax rules.
JsonWriter name(String name)
Encodes the property name.
JsonWriter nullValue()
Encodes null.
void setIndent(String indent)
Sets the indentation string to be repeated for each level of indentation in the encoded document.
void setLenient(boolean lenient)
Configure this writer to relax its syntax rules.
JsonWriter value(double value)
Encodes value.
JsonWriter value(long value)
Encodes value.
JsonWriter value(Number value)
Encodes value.
JsonWriter value(boolean value)
Encodes value.
JsonWriter value(String value)
Encodes value.

我们平时需要注意的就是API Level为honycomb才能调用,同时上面代码为utf-8编码,写入时保证流清空记住调用flush方法将缓存写入到流中。