JSON 是非常常用的数据传输格式,在 Android 中也内置了解析 JSON 的工具类,今天就来详细的学习一下。

JSON 的定义

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。

以上就是 JSON 的描述,更为详细的资料可以参见JSON官网

JSON建构于两种结构:

  1. “名称/值”对的集合(A collection of name/value pairs)。

    不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。

  2. 值的有序列表(An ordered list of values)。

    在大部分语言中,它被理解为数组(array)。

从 Android doc 中我们可以看到内置的两套 JSON 解析类库,而在实际开发中 GSON 的使用率也非常高,下面就来挨个看看。

org.json

org.json 是从 API 1 就开始内置的 JSON 解析类库。

JSONObject

JSONObject 对应的就是 json 对象

                         //{'name':'kass','age':24,'male':true}"                     public static void jsonObjectTest() throws JSONException {                                           JSONObject jsonObject1 = new JSONObject();                      jsonObject1.put("name", "kass").put("age", 24).put("male", true);                      System.out.println(jsonObject1.toString());                                           JSONObject jsonObject2 = new JSONObject(                      "{'name':'kass','age':24,'male':true}");                      System.out.println(jsonObject2.get("male"));                      System.out.println(jsonObject2.get("age"));                      System.out.println(jsonObject2.get("name"));                     }            

JSONArray

JSONArray 对应的就是 json 数组对象,构建 json 数组要先创建好内部的JSONObjectd对象

                         // [{'name':'kass','age':24,'male':true},{'name':'tutu','age':20,'male':false}]                      public static void jsonArrayTest() throws JSONException {                      // 先创建内部的两个 JSONObject                      JSONObject jsonObject1 = new JSONObject();                      jsonObject1.put("name", "kass").put("age", 24).put("male", true);                                           JSONObject jsonObject2 = new JSONObject();                      jsonObject2.put("name", "tutu").put("age", 20).put("male", false);                                           // 再创建JSONArray,将之前的两个JSONObject添加近来                      JSONArray jsonArray1 = new JSONArray();                      jsonArray1.put(jsonObject1).put(jsonObject2);                      System.out.println(jsonArray1.toString(4));                                           // 解析 json 数组                      JSONArray jsonArray2 = new JSONArray("[{'name':'kass','age':24,'male':true},{'name':'tutu','age':20,'male':false}]");                      JSONObject temp = jsonArray2.getJSONObject(0);                      System.out.println(temp.getString("name"));                                           // 遍历 json 数组                      for (int i = 0; i < jsonArray2.length(); i++) {                      temp = jsonArray2.getJSONObject(i);                      System.out.println(temp.toString());                      System.out.println(temp.opt("male"));                      }                     }            

Java null 和 JSONObject.NULL

JSONObject 文档中有如下警告:

Warning: this class represents null in two incompatible ways: the standard Java null reference, and the sentinel value JSONObject.NULL. In particular, calling put(name, null) removes the named entry from the object but put(name, JSONObject.NULL) stores an entry whose value is JSONObject.NULL.

put(name, null) 会移除name键值对
put(name, JSONObject.NULL) 则会输出为 "name":null

                         private static void testNull() throws JSONException {                      JSONObject jsonObject1 = new JSONObject();                      jsonObject1.put("name", null);                      System.out.println(jsonObject1.toString());                      jsonObject1.put("name", JSONObject.NULL);                      System.out.println(jsonObject1.toString());                     }            

其他

JSONStringer 是 JSONObject.toString()、JSONArray.toString()的底层方法了,可以用于生成 json 字符串

JSONTokener 是 JSONObject 是用来解析 json 字符串的,我们可以在 JSONObject 的构造方法里看到,一般也用不到了

JSONObject 获取内容时有两套 get 方法:

  1. getType 可以将要获取的键的值转换为指定的类型,如果无法转换或没有值则抛出JSONException
  2. optType 也是将要获取的键的值转换为指定的类型,无法转换或没有值时返回用户提供或这默认提供的值

可以看出来 org.json 提供的解析方法写起来略麻烦,下篇说一下 Android 3.0 后添加的另一个 JSON 解析类库

更多相关文章

  1. Android中监听语言变化的两种方式
  2. Android接口定义语言---AIDL(一)
  3. Android NDK:JNI 数组的输入输出
  4. android将对象写入文件和从文件中读取对象数据
  5. 我的Android进阶之旅------>Android 设置默认语言、默认时区
  6. Android如何判断系统语言
  7. android byte[]数组,bitmap,drawable之间的相互转换
  8. android 之json对象解析并展示(含json解析源码)

随机推荐

  1. 怎么查看android 版本
  2. android 用户注册demo
  3. android销毁Activity和被系统销毁时的数
  4. 【按键】灭屏后瞬间按返回键,可以点亮屏幕
  5. Shape属性详解
  6. android关于获取手机唯一码的组合文章
  7. 【android】get PatchStore::createDisab
  8. Android软键盘手动显示、隐藏、布局上移
  9. Android(安卓)TableLayout表格布局
  10. Android aapt 工具介绍(转载)