Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 中階開發

Android JSON 解析類別 - JsonReader

編輯:中階開發

從 Android 3.0 Honeycomb 開始提供了全新的 JSON 解析類別 android.util.JsonReader,下面 Android123 以這段 JSON 為例子:

[
   {
     "id": 912345678901,
     "text": "How do I read JSON on android?",
     "geo": null,
     "user": {
       "name": "android_newb",
       "followers_count": 41
      
   },
   {
     "id": 912345678902,
     "text": "@android_newb just use android.util.JsonReader!",
     "geo": [50.454722, -104.606667],
     "user": {
       "name": "jesse",
       "followers_count": 2
     }
   }
 ]

要解析上面的 JSON,使用下面程式碼即可,整個處理方法和解析 XML 差不多,最終用 List 陣列保存。不過 Android 開發網提醒,下面的編碼為 UTF-8,如果遇到中文而伺服器預設按 GBK 編碼,把 UTF-8 改為 GB2312 就能解決亂碼問題。

 public List readJsonStream(InputStream in) throws IOException {
     JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
     return readMessagesArray(reader);
   
 
   public List readMessagesArray(JsonReader reader) throws IOException {
     List messages = new ArrayList();
 
     reader.beginArray();
     while (reader.hasNext()) {
       messages.add(readMessage(reader));
     }
     reader.endArray();
     return messages;
   }
 
   public Message readMessage(JsonReader reader) throws IOException {
     long id = -1;
     String text = null;
     User user = null;
     List geo = null;
 
     reader.beginObject();
     while (reader.hasNext()) {
       String name = reader.nextName();
       if (name.equals("id")) {
         id = reader.nextLong();
       } else if (name.equals("text")) {
         text = reader.nextString();
       } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
         geo = readDoublesArray(reader);
       } else if (name.equals("user")) {
         user = readUser(reader);
       } else {
         reader.skipValue();
       }
     }
     reader.endObject();
     return new Message(id, text, user, geo);
   }
 
   public List readDoublesArray(JsonReader reader) throws IOException {
     List doubles = new ArrayList();
 
     reader.beginArray();
     while (reader.hasNext()) {
       doubles.add(reader.nextDouble());
     }
     reader.endArray();
     return doubles;
   }
 
   public User readUser(JsonReader reader) throws IOException {
     String username = null;
     int followersCount = -1;
 
     reader.beginObject();
     while (reader.hasNext()) {
       String name = reader.nextName();
       if (name.equals("name")) {
         username = reader.nextString();
       } else if (name.equals("followers_count")) {
         followersCount = reader.nextInt();
       } else {
         reader.skipValue();
       }
     }
     reader.endObject();
     return new User(username, followersCount);
   }

最後 Android123 再次提醒:JsonReader 是 Android 3.0 引入的新解析類別,必須在 API Level 為 Honeycomb 的 SDK 以及 3.0 以上的固件上才能使用,完整的成員如下:

Public Constructors
 JsonReader(Reader in)  公共構造方法
 
void  beginArray()
Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.
void  beginObject()
Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.
void  close()
Closes this JSON reader and the underlying Reader.
void  endArray()
Consumes the next token from the JSON stream and asserts that it is the end of the current array.
void  endObject()
Consumes the next token from the JSON stream and asserts that it is the end of the current object.
boolean  hasNext()
Returns true if the current array or object has another element.
boolean  isLenient()
Returns true if this parser is liberal in what it accepts.
boolean  nextBoolean()
Returns the boolean value of the next token, consuming it.
double  nextDouble()
Returns the double value of the next token, consuming it.
int  nextInt()
Returns the int value of the next token, consuming it.
long  nextLong()
Returns the long value of the next token, consuming it.
String  nextName()
Returns the next token, a property name, and consumes it.
void  nextNull()
Consumes the next token from the JSON stream and asserts that it is a literal null.
String  nextString()
Returns the string value of the next token, consuming it.
JsonToken  peek()
Returns the type of the next token without consuming it.
void  setLenient(boolean lenient)
Configure this parser to be liberal in what it accepts.
void  skipValue()
Skips the next value recursively.
String  toString()
Returns a string containing a concise, human-readable description of this object.

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved