編輯:高級開發
[
{
"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 array.
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 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.
即: eclipse-Java-heliOS-SR2-win32-x86_64.zip 這個文件。 下載後解壓縮後就可以用了。 使用時選擇一個Workspace 即
Google的開源android移動操作系統正在席卷全球智能手機市場,和蘋果不一樣,它對那些想將應用程序提交到iPhone App Store的開發人員有著嚴格的指導方
android平台的項目目前正在從手機運營商、手機廠商、開發者和消費者那裡獲得大力的支持,在這期間,很有可能會迎來 android程序的大爆發,畢竟這個系統太強大了。一
過去的2010年是android全面爆發的一年,出眾的擴展性使其成為了眾多玩家的購機首選,市場占有率節節攀高。本文與大家分享七個非常有用的android開發工具和工具包