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

Android JSON寫入類 - JsonWriter

編輯:Android開發實例

   在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