編輯:中階開發
從 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.
IBM Rational Rhapsody V7.5.2 新功能解析:多核、Android、C# 與系統工程
版本定位 IBM Rational Rhapsody V7.5.2 針對系統工程、嵌入式軟體開發與測試流程進行擴充,重點包含多核處理器建模、Android 應用程式可視化
Android 3.0 Honeycomb 底層細節:硬體加速、Renderscript 與平板 UI
Honeycomb:Android 平板時代的起點 Android 3.0,代號 Honeycomb,是 Android 第一個主要針對平板設計的版本,也是首個導入硬體加
Android 本機資料儲存 API 入門:Preference、SQLite 與內部外部儲存
許多 Android 應用都需要保存資料,例如使用者偏好、設定值、好友清單或圖片。若能在裝置本機完成儲存與讀取,就能降低對網路的依賴,也能提升使用體驗。本文整理 Andr
ListView 快速滾動滑塊的啟用方式與顯示條件
長清單滑到一半想直接跳到某個位置時,右側那條快速滾動滑塊就派上用場了。很多人不知道它怎麼開啟,其實只要一個屬性。 XML 布局 在 ListView 節點中加入: an