说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

Android JSON解析类 - JsonReader

HTML文档下载 WORD文档下载 PDF文档下载
Android JSON解析类 - JsonReader

作者:Android开发网


   在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 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.

备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘