Android JSON解析类 - JsonReader

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:
 在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.

相关文章
|
6天前
|
安全 Android开发 iOS开发
安卓与iOS的较量:技术特性与用户体验的深度解析
在移动操作系统的战场上,安卓和iOS一直占据着主导地位。本文将深入探讨这两大平台的核心技术特性,以及它们如何影响用户的体验。我们将从系统架构、应用生态、安全性能和创新功能四个方面进行比较,帮助读者更好地理解这两个系统的异同。
34 3
|
13天前
|
存储 JSON API
淘系API接口(解析返回的json数据)商品详情数据解析助力开发者
——在成长的路上,我们都是同行者。这篇关于商品详情API接口的文章,希望能帮助到您。期待与您继续分享更多API接口的知识,请记得关注Anzexi58哦! 淘宝API接口(如淘宝开放平台提供的API)允许开发者获取淘宝商品的各种信息,包括商品详情。然而,需要注意的是,直接访问淘宝的商品数据API通常需要商家身份或开发者权限,并且需要遵循淘宝的API使用协议。
淘系API接口(解析返回的json数据)商品详情数据解析助力开发者
|
11天前
|
缓存 Java 开发者
Spring高手之路22——AOP切面类的封装与解析
本篇文章深入解析了Spring AOP的工作机制,包括Advisor和TargetSource的构建与作用。通过详尽的源码分析和实际案例,帮助开发者全面理解AOP的核心技术,提升在实际项目中的应用能力。
9 0
Spring高手之路22——AOP切面类的封装与解析
|
19天前
|
JSON Java API
在 Java 中解析 JSON ArrayList 的详细指南
【8月更文挑战第23天】
19 1
|
19天前
|
JSON 开发框架 JavaScript
【Azure Developer】使用.Net Core解析JSON的笔记
【Azure Developer】使用.Net Core解析JSON的笔记
|
3天前
|
存储 JSON API
Python编程:解析HTTP请求返回的JSON数据
使用Python处理HTTP请求和解析JSON数据既直接又高效。`requests`库的简洁性和强大功能使得发送请求、接收和解析响应变得异常简单。以上步骤和示例提供了一个基础的框架,可以根据你的具体需求进行调整和扩展。通过合适的异常处理,你的代码将更加健壮和可靠,为用户提供更加流畅的体验。
20 0
|
11天前
|
图形学 iOS开发 Android开发
从Unity开发到移动平台制胜攻略:全面解析iOS与Android应用发布流程,助你轻松掌握跨平台发布技巧,打造爆款手游不是梦——性能优化、广告集成与内购设置全包含
【8月更文挑战第31天】本书详细介绍了如何在Unity中设置项目以适应移动设备,涵盖性能优化、集成广告及内购功能等关键步骤。通过具体示例和代码片段,指导读者完成iOS和Android应用的打包与发布,确保应用顺利上线并获得成功。无论是性能调整还是平台特定的操作,本书均提供了全面的解决方案。
61 0
|
11天前
|
开发者 算法 虚拟化
惊爆!Uno Platform 调试与性能分析终极攻略,从工具运用到代码优化,带你攻克开发难题成就完美应用
【8月更文挑战第31天】在 Uno Platform 中,调试可通过 Visual Studio 设置断点和逐步执行代码实现,同时浏览器开发者工具有助于 Web 版本调试。性能分析则利用 Visual Studio 的性能分析器检查 CPU 和内存使用情况,还可通过记录时间戳进行简单分析。优化性能涉及代码逻辑优化、资源管理和用户界面简化,综合利用平台提供的工具和技术,确保应用高效稳定运行。
24 0
|
16天前
|
JSON API 数据格式
基于服务器响应的实时天气数据进行JSON解析的详细代码及其框架
【8月更文挑战第25天】这段资料介绍了一个使用Python从服务器获取实时天气数据并解析JSON格式数据的基本框架。主要分为三个部分:一是安装必要的`requests`库以发起HTTP请求获取数据,同时利用Python内置的`json`库处理JSON数据;二是提供了具体的代码实现,包括获取天气数据的`get_weather_data`函数和解析数据的`parse_weather_data`函数;三是对代码逻辑进行了详细说明,包括如何通过API获取数据以及如何解析这些数据来获取温度和天气描述等信息。用户需要根据实际使用的天气API调整代码中的API地址、参数和字段名称。
|
18天前
|
JSON 数据格式 索引
【Azure Developer】Azure Logic App 示例: 解析 Request Body 的 JSON 的表达式? triggerBody()?
【Azure Developer】Azure Logic App 示例: 解析 Request Body 的 JSON 的表达式? triggerBody()?

热门文章

最新文章

推荐镜像

更多