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.

相关文章
|
12天前
|
存储 Java API
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
56 0
|
12天前
|
安全 Java 定位技术
Android 浅度解析:AIDL & Binder (1)
Android 浅度解析:AIDL & Binder (1)
38 0
|
5天前
|
JSON 安全 Swift
【Swift开发专栏】Swift中的JSON解析与处理
【4月更文挑战第30天】本文介绍了Swift中的JSON解析与处理。首先,讲解了JSON的基础,包括其键值对格式和在Swift中的解析与序列化方法。接着,展示了如何使用`Codable`协议简化JSON操作,以及处理复杂结构的示例。通过这些内容,读者能掌握在Swift中高效地处理JSON数据的方法。
|
5天前
|
前端开发 测试技术 数据处理
安卓开发中的MVP架构模式深度解析
【4月更文挑战第30天】在移动应用开发领域,模型-视图-呈现器(Model-View-Presenter, MVP)是一种广泛采用的架构模式。它旨在通过解耦组件间的直接交互来提高代码的可维护性和可测试性。本文将深入探讨MVP在安卓开发中的应用,揭示其如何促进代码的模块化,提升用户界面的响应性,并简化单元测试过程。我们将从理论概念出发,逐步过渡到实践案例,为读者提供一套行之有效的MVP实施策略。
|
5天前
|
分布式计算 DataWorks 关系型数据库
DataWorks产品使用合集之在DataWorks中,使用JSON解析函数将MySQL表中的字段解析成多个字段将这些字段写入到ODPS(MaxCompute)中如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
14 3
|
12天前
|
存储 物联网 数据库
Android 11 以上 SettingsProvider DatabaseHelper 解析
Android 11 以上 SettingsProvider DatabaseHelper 解析
18 0
|
12天前
|
安全 Java Shell
Android13 adb input 调试命令使用和源码解析
Android13 adb input 调试命令使用和源码解析
19 0
|
12天前
|
XML Java API
Android 浅度解析:系统框架层修改,编译,推送相关操作
Android 浅度解析:系统框架层修改,编译,推送相关操作
24 0
|
12天前
|
JSON 前端开发 Java
SpringBoot之JSON参数,路径参数的详细解析
SpringBoot之JSON参数,路径参数的详细解析
13 0
|
21天前
|
编解码 Android开发 开发者
安卓碎片(Fragments)深度解析:模块化您的应用
【4月更文挑战第13天】Android开发中,碎片(Fragments)用于应对多屏幕尺寸挑战,实现代码重用和模块化。碎片可嵌套在活动中,适应不同屏幕,简化多屏适配、导航和视图管理。通过`FragmentTransaction`动态操作,实现状态保存及回退栈管理。注意避免内存泄漏,考虑性能开销,确保跨设备测试。碎片增强应用灵活性,但也增加复杂性,未来有望有更高级工具优化开发。

推荐镜像

更多