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

相关文章
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
52 1
|
1月前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
269 0
|
14天前
|
存储 JSON JavaScript
「Python系列」Python JSON数据解析
在Python中解析JSON数据通常使用`json`模块。`json`模块提供了将JSON格式的数据转换为Python对象(如列表、字典等)以及将Python对象转换为JSON格式的数据的方法。
31 0
|
1月前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
95 2
|
1月前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
52 0
|
1月前
|
JSON 数据格式
人脸检测解析json的工具类face_test
人脸检测解析json的工具类face_test
14 0
|
1月前
|
JSON JavaScript 前端开发
如何在Python中解析JSON响应?
【2月更文挑战第26天】【2月更文挑战第92篇】如何在Python中解析JSON响应?
|
1月前
|
JSON JavaScript 前端开发
Qt 5.14.2 深度解析:打造高效JSON处理利器
Qt 5.14.2 深度解析:打造高效JSON处理利器
|
1天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
10天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
19 0

推荐镜像

更多