JSON

Android Json解析与总结

字号+ 作者:H5之家 来源:H5之家 2017-10-03 10:00 我要评论( )

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建构于两种结构:
“名称/值”对的集合(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官网》
三、JSON解析常用类

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

JSONObject、JSONArray、JSONException、JSONStringer、JSONTokener
     

2. 常见方法

使用get方法与使用opt方法的区别?

JsonObject 方法,opt* 与 get* 建议使用opt方法,因为get方法如果其内容为空会直接抛出异常。不过JsonArray.opt*(index)会有越界问题需要特别注意。

opt、optBoolean、optDouble、optInt、optLong、optString、optJSONArray、optJSONObject get、getBoolean、getDouble、getInt、getLong、getString、getJSONArray、getJSONObject


3. Android创建JSON

private String createJson() throws JSONException { JSONObject jsonObject = new JSONObject(); jsonObject.put ("intKey" , 123); jsonObject.put ("doubleKey" , 10.1); jsonObject.put ("longKey" , 666666666); jsonObject.put ("stringKey" , "lalala" ); jsonObject.put ("booleanKey" , true); JSONArray jsonArray = new JSONArray(); jsonArray.put (0, 111); jsonArray.put("second"); jsonObject.put ("arrayKey" , jsonArray); JSONObject innerJsonObject = new JSONObject(); innerJsonObject.put ("innerStr" , "inner" ); jsonObject.put ("innerObjectKey" , innerJsonObject); Log.e("Json" , jsonObject.toString()); return jsonObject.toString(); }

输出结果:

{"intKey":123, "doubleKey":10.1, "longKey":666666666, "stringKey":"lalala", "booleanKey":true, "arrayKey":[111,"second"], "innerObjectKey":{"innerStr":"inner"}}




4. 解析上面创建的JSON

private void pareJson(String jsonStr) throws JSONException { JSONObject jsonObject = new JSONObject(jsonStr); int intValue = jsonObject.optInt( "intKey"); double doubleValue = jsonObject.optDouble( "doubleKey"); long longValue = jsonObject.optLong( "longKey"); String strValue = jsonObject.optString( "stringKey"); boolean boolValue = jsonObject.optBoolean( "booleanKey"); JSONArray array = jsonObject.optJSONArray( "arrayKey"); int arrIntValue = array.optInt(0); String arrStrValue = array.optString(1); JSONObject innerJson = jsonObject.optJSONObject("innerObjectKey" ); String innerStr = innerJson.optString( "innerStr"); Log.e("Json" , "intValue = " + intValue + " , doubleValue = " + doubleValue + " , longValue = " + longValue + " , strValue = " + strValue + " , booleanValue = " + boolValue + " , arrIntValue = " + arrIntValue + " , arrStrValue = " + arrStrValue + " , innerStr = " + innerStr); }

输出结果:

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
《两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)》
五、格式化工具

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • bbs.bjsxt.com

    bbs.bjsxt.com

    2017-10-03 09:05

  • [教学视频]Qt项目实战之网络电子白板

    [教学视频]Qt项目实战之网络电子白板

    2017-10-02 17:15

  • google Gson学习笔记及与json

    google Gson学习笔记及与json

    2017-10-02 16:05

  • 关于JSONObject与GSON解析JSON数据详解

    关于JSONObject与GSON解析JSON数据详解

    2017-10-02 10:02

网友点评
i