Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android JSON寫入類 - JsonWriter

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方法將緩存寫入到流中。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved