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建构于两种结构:
“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

JSON具有以下这些形式:

对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。


数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。


值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。


三、JSON解析常用类

1. Android JSON所有相关类,都在org.json包下

JSONObject、JSONArray、JSONException、JSONStringer、JSONTokener

2. 常见方法
使用get方法与使用opt方法的区别?
JsonObject 方法,opt* 与 get* 建议使用opt方法,因为get方法如果其内容为空会直接抛出异常。不过JsonArray.opt*(index)会有越界问题需要特别注意。
[java]  view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. opt、optBoolean、optDouble、optInt、optLong、optString、optJSONArray、optJSONObject  
  2. get、getBoolean、getDouble、getInt、getLong、getString、getJSONArray、getJSONObject  
3. Android创建JSON
[java]  view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. private String createJson() throws JSONException {  
  2.  JSONObject jsonObject = new JSONObject();  
  3.  jsonObject.put ("intKey" , 123);  
  4.  jsonObject.put ("doubleKey" , 10.1);  
  5.  jsonObject.put ("longKey" , 666666666);  
  6.  jsonObject.put ("stringKey" , "lalala" );  
  7.  jsonObject.put ("booleanKey" , true);  
  8.    
  9.  JSONArray jsonArray = new JSONArray();  
  10.  jsonArray.put (0111);  
  11.  jsonArray.put("second");  
  12.  jsonObject.put ("arrayKey" , jsonArray);  
  13.    
  14.  JSONObject innerJsonObject = new JSONObject();  
  15.  innerJsonObject.put ("innerStr" , "inner" );  
  16.  jsonObject.put ("innerObjectKey" , innerJsonObject);  
  17.    
  18.    
  19.  Log.e("Json" , jsonObject.toString());  
  20.    
  21.  return jsonObject.toString();  
  22.  }  
输出结果:
{"intKey":123, "doubleKey":10.1, "longKey":666666666, "stringKey":"lalala", "booleanKey":true, "arrayKey":[111,"second"], "innerObjectKey":{"innerStr":"inner"}}
4. 解析上面创建的JSON
[java]  view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. private void pareJson(String jsonStr) throws JSONException {  
  2.  JSONObject jsonObject = new JSONObject(jsonStr);  
  3.  int intValue       = jsonObject.optInt( "intKey");  
  4.  double doubleValue = jsonObject.optDouble( "doubleKey");  
  5.  long longValue     = jsonObject.optLong( "longKey");  
  6.  String strValue    = jsonObject.optString( "stringKey");  
  7.  boolean boolValue  = jsonObject.optBoolean( "booleanKey");  
  8.    
  9.  JSONArray array    = jsonObject.optJSONArray( "arrayKey");  
  10.  int arrIntValue    = array.optInt(0);  
  11.  String arrStrValue = array.optString(1);  
  12.    
  13.  JSONObject innerJson = jsonObject.optJSONObject("innerObjectKey" );  
  14.  String innerStr      = innerJson.optString( "innerStr");  
  15.    
  16.  Log.e("Json" , "intValue = " + intValue + " , doubleValue = " + doubleValue  
  17.             + " , longValue = " + longValue + " , strValue = " + strValue  
  18.             + " , booleanValue = " + boolValue + " , arrIntValue = " + arrIntValue  
  19.             + " , arrStrValue = " + arrStrValue + " , innerStr = " + innerStr);  
  20. }  
输出结果:
intValue = 123 , doubleValue = 10.1 , longValue = 666666666 , strValue = lalala , booleanValue = true , arrIntValue = 111 , arrStrValue = second , innerStr = inner
更多具体信息详见:
《android json解析及简单例子》
《Android学习笔记44:JSON数据解析》

四、Android JSON解析库

    上面介绍都是使用Android提供的原生类解析JSON,最大的好处是项目不需要引入第三方库,但是如果比较注重开发效率而且不在意应用大小增加几百K的话,有以下JSON可供选择:

1. Jackson
2. google-gson
3. Json-lib

五、格式化工具

    在日常开发中,如果涉及到与服务器端调试协议时,过程中难免遇到服务器端发送格式或者Android客户端解析格式出现问题,这时需要把获取到的JSON打印出来进行问题定位,如果JSON比较短一眼就能看出来,但是如果很长的话想查找某一个字段或者JSON数组中某一位的值显得特别困难,想要摆脱这种苦恼也很简单,把JSON字符串格式化之后会发现苦恼瞬间无影无踪。以下是以我常用的Notepad++进行举例,其他的编辑器也肯定有相应的JSON插件,自己可以网上查找一下。

Notpad++ Json Viewer插件
安装:
Notpad++ -> 插件(Plugins) -> Plugin Manager -> Show Plugin Manager -> Avaliable -> 选择Json View -> 安装(install)
使用:
选中Json字符串,
插件(Plugins) -> Format Json(快捷键Ctrl+Alt+Shift+M)
插件(Plugins) -> Show Json Viewer  显示Json视图

转载于:https://my.oschina.net/huewenhua/blog/681641

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值