android jsonwriter
来源:互联网 发布:mac双系统怎么切换系统 编辑:程序博客网 时间:2024/05/29 09:05
在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方法将缓存写入到流中。
- android jsonwriter
- Android JSON 解析(二) JSONReader 和 JSONWriter
- Android读写JSON格式的数据之JsonWriter和JsonReader
- Android读写JSON格式的数据之JsonWriter和JsonReader
- JSONWriter.java
- JsonWriter的使用
- JsonWriterFactory、JsonWriter源码翻译
- org.apache.struts2.json.JSONWriter
- JsonReader解析json和JsonWriter生成json
- 问题解决Java heap space——at org.apache.struts2.json.JSONWriter.add(JSONWriter.java:511)
- 解决Jsonwriter发生的Positioned Update not supported问题
- Class org.apache.struts2.json.JSONWriter can not access a member of *(转)
- Class org.apache.struts2.json.JSONWriter can not access a member of *
- Class org.apache.struts2.json.JSONWriter can not access a member of *
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- Class org.apache.struts2.json.JSONWriter can not access a member of *
- Class org.apache.struts2.json.JSONWriter can not access a member of *
- MyEclipse默认标签TODO,XXX,FIXME和自定义标签的使用
- 获取checkedListBox被选定组合字符串方法
- configure: error: newly created file is older than distributed files! 错误处理
- 移植FFMPEG到VS2008系列之四
- git使用(4)理解和使用分支
- android jsonwriter
- C#操作JSON
- jquery弹出遮罩层(可随滚动条而移动的)
- jQuery实现全选和反选和临时页面删除
- java类加载器总结
- Linux内核模块LKM编译-自制Makefile模板
- IOS Cocos2dx 添加Admob 广告错误
- 实验任务一
- 有趣的地址