Json:一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。 – Json.org

 

JSON Vs XML

1.JSON和XML的数据可读性基本相同

2.JSON和XML同样拥有丰富的解析手段

3.JSON相对于XML来讲,数据的体积小

4.JSON与JavaScript的交互更加方便

5.JSON对数据的描述性比XML较差

6.JSON的速度要远远快于XML

一、JSON语法

 

  JSON 语法规则

 

  • 数据在名称/值对中
  • 数据由逗号分隔
  • 花括号保存对象
  • 方括号保存数组

 

JSON 名称/值对

   JSON 数据的书写格式是:名称/值对。

   名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:

"firstName" : "John"


 JSON 值

  JSON 值可以是:

  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在方括号中)
  • 对象(在花括号中)
  • null
  • JSONObject
  • JSONArray


JSON 对象

JSON 对象在花括号中书写:

对象可以包含多个名称/值对:

{ "firstName":"John" , "lastName":"Doe" }
一个{}就是一个JSONObject



JSON 数组

JSON 数组在方括号中书写:

数组可包含多个对象:

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}


在上面的例子中,对象 "employees" 是包含三个对象的数组。每个对象代表一条关于某人(有姓和名)的记录。


二、android提供的json解析类 

android的json解析部分都在包org.json下,主要有以下几个类: 


JSONObject:可以看作是一个json对象,这是系统中有关JSON定义的基本单元,其包含一对儿(Key/Value)数值。它对外部(External:   应用toString()方法输出的数值)调用的响应体现为一个标准的字符串(例如:{"JSON": "Hello, World"},最外被大括号包裹,其中的Key和Value被冒号":"分隔)。其对于内部(Internal)行为的操作格式略微,例如:初始化一个JSONObject实例,引用内部的put()方法添加数值:new JSONObject().put("JSON", "Hello, World!"),在Key和Value之间是以逗号","分隔。Value的类型包括:Boolean、JSONArray、JSONObject、Number、String或者默认值JSONObject.NULLobject 。

JSONStringer:json文本构建类 ,根据官方的解释,这个类可以帮助快速和便捷的创建JSON text。其最大的优点在于可以减少由于格式的错误导致程序异常,引用这个类可以自动严格按照JSON语法规则(syntax rules)创建JSON text。每个JSONStringer实体只能对应创建一个JSON text。

JSONArray:它代表一组有序的数值。将其转换为String输出(toString)所表现的形式是用方括号包裹,数值以逗号”,”分隔(例如:     [value1,value2,value3],大家可以亲自利用简短的代码更加直观的了解其格式)。这个类的内部同样具有查询行为,     get()和opt()两种方法都可以通过index索引返回指定的数值,put()方法用来添加或者替换数值。同样这个类的value类型可以包括:Boolean、JSONArray、JSONObject、Number、String或者默认值JSONObject.NULL object。

JSONTokener:json解析类 

JSONException:json中用到的异常 

1.JSONObject,JSONArray解析,创建Json

示例代码:





1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38




​/* ​


​json:{ ​


​"languages":[ ​


​{"id":1,"ide":"Eclispe","name":"java"}, ​


​{"id":2,"ide":"Xcode","name":"Swift"}, ​


​{"id":3,"ide":"Visual Studio","name":"C++"}], ​


​"cat":{"cat":"miao"} ​


​} ​


​*/​​  


​public​​ ​​void​​ ​​creatJson2(){  ​


 


​try​​ ​​{  ​


​JSONObject root = ​​​​new​​ ​​JSONObject();  ​


​JSONObject cat = ​​​​new​​ ​​JSONObject();  ​


​cat.put(​​​​"cat"​​​​, ​​​​"miao"​​​​);  ​


​JSONArray array = ​​​​new​​ ​​JSONArray();  ​


 


​JSONObject lan1 = ​​​​new​​ ​​JSONObject();  ​


​lan1.put(​​​​"id"​​​​, ​​​​1​​​​).put(​​​​"ide"​​​​, ​​​​"Eclispe"​​​​).put(​​​​"name"​​​​, ​​​​"java"​​​​);  ​


 


​JSONObject lan2 = ​​​​new​​ ​​JSONObject();  ​


​lan2.put(​​​​"id"​​​​, ​​​​2​​​​).put(​​​​"ide"​​​​, ​​​​"Xcode"​​​​).put(​​​​"name"​​​​, ​​​​"Swift"​​​​);  ​


​JSONObject lan3 = ​​​​new​​ ​​JSONObject();  ​


​lan3.put(​​​​"id"​​​​, ​​​​3​​​​).put(​​​​"ide"​​​​, ​​​​"Visual Studio"​​​​).put(​​​​"name"​​​​, ​​​​"C++"​​​​);  ​


​array.put(lan1);  ​


​array.put(lan2);  ​


​array.put(lan3);  ​


 


​root.put(​​​​"languages"​​​​, array);  ​


​root.put(​​​​"cat"​​​​, cat);  ​


 


​System.out.println(​​​​"json:"​​​​+root.toString());  ​


 


​} ​​​​catch​​ ​​(JSONException e) {  ​


​// TODO Auto-generated catch block  ​


​e.printStackTrace();  ​


​}  ​


​}​



  然后是解析的代码:





1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32




​public​​ ​​void​​ ​​parseJson(){  ​


​try​​ ​​{  ​


​InputStreamReader is = ​​​​new​​ ​​InputStreamReader(getAssets().open(​​​​"test2.json"​​​​), ​​​​"UTF-8"​​​​);  ​


​BufferedReader br = ​​​​new​​ ​​BufferedReader(is);  ​


​String line;  ​


​StringBuilder builder = ​​​​new​​ ​​StringBuilder();  ​


​while​​​​((line=br.readLine())!=​​​​null​​​​){  ​


​builder.append(line);  ​


​}  ​


​is.close();br.close();  ​


​JSONObject root = ​​​​new​​ ​​JSONObject(builder.toString());  ​


​System.out.println(​​​​"cat:"​​​​+root.getString(​​​​"cat"​​​​));  ​


​JSONArray array = root.getJSONArray(​​​​"languages"​​​​);  ​


​for​​​​(​​​​int​​ ​​i=​​​​0​​​​;i<array.length();i++){  ​


​JSONObject lan = array.getJSONObject(i);  ​


​System.out.println(​​​​".........."​​​​);  ​


​System.out.println(​​​​"id="​​​​+lan.getInt(​​​​"id"​​​​));  ​


​System.out.println(​​​​"ide="​​​​+lan.getString(​​​​"ide"​​​​));  ​


​System.out.println(​​​​"name="​​​​+lan.getString(​​​​"name"​​​​));  ​


​}  ​


 


​} ​​​​catch​​ ​​(UnsupportedEncodingException e) {  ​


​// TODO Auto-generated catch block  ​


​e.printStackTrace();  ​


​} ​​​​catch​​ ​​(IOException e) {  ​


​// TODO Auto-generated catch block  ​


​e.printStackTrace();  ​


​}​​​​catch​​ ​​(JSONException e) {  ​


​// TODO Auto-generated catch block  ​


​e.printStackTrace();  ​


​}  ​


​}​



   这时解析的源文件:





1


2


3


4


5


6


7


8




​{  ​


​"languages"​​​​:[  ​


​{​​​​"id"​​​​:​​​​1​​​​,​​​​"ide"​​​​:​​​​"Eclipse"​​​​,​​​​"name"​​​​:​​​​"java"​​​​},  ​


​{​​​​"id"​​​​:​​​​2​​​​,​​​​"ide"​​​​:​​​​"Xcode"​​​​,​​​​"name"​​​​:​​​​"Swift"​​​​},  ​


​{​​​​"id"​​​​:​​​​3​​​​,​​​​"ide"​​​​:​​​​"Visual Studio"​​​​,​​​​"name"​​​​:​​​​"C++"​​​​}  ​


​],  ​


​"cat"​​​​:​​​​"miao"​​ 


​}  ​



  

2.JSONStringer生成json 

Stringers only encode well-formed JSON strings. In particular:

  • The stringer must have exactly one top-level array or object.
  • Lexical scopes must be balanced: every call to ​​array()​​ must have a matching call to ​​endArray()​​ and every call to ​​object()​​ must have a matching call to ​​endObject()​​.  //每次调用array(),必须匹配endArray,object,endObject同理。
  • Arrays may not contain keys (property names).
  • Objects must alternate keys (property names) and values.
  • Values are inserted with either literal ​​value​​ calls, or by nesting arrays or objects.

它定义的所有方法:

它定义的所有方法:



Public Constructors

 

​JSONStringer​​()


Public Methods


​JSONStringer​​ ​​array​​()

Begins encoding a new array.



​JSONStringer​​ ​​endArray​​()

Ends encoding the current array.



​JSONStringer​​ ​​endObject​​()

Ends encoding the current object.



​JSONStringer​​ ​​key​​(​​String​​ name)

Encodes the key (property name) to this stringer.



​JSONStringer​​ ​​object​​()

Begins encoding a new object.



​String​​ ​​toString​​()

Returns the encoded JSON string.



​JSONStringer​​ ​​value​​(double value)

Encodes ​​value​​ to this stringer.



​JSONStringer​​ ​​value​​(​​Object​​ value)

Encodes ​​value​​.



​JSONStringer​​ ​​value​​(long value)

Encodes ​​value​​ to this stringer.



​JSONStringer​​ ​​value​​(boolean value)

Encodes ​​value​​ to this stringer.


它的方法不多,很精简,所以说用Stringer创建json还是很简单的。
示例代码:





1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


40


41


42


43




​/*json:{ ​


​"languages":[ ​


​{"id":1,"ide":"Eclispe","name":"java"}, ​


​{"id":2,"ide":"Xcode","name":"Swift"}, ​


​{"id":3,"ide":"Visual Studio","name":"C++"}], ​


​"cat":{"name":"miao"} ​


​}*/​​  


​public​​ ​​String createJson(){  ​


​JSONStringer stringer = ​​​​new​​ ​​JSONStringer();  ​


​//every call to array() must have a matching call to endArray() and  ​


​//every call to object() must have a matching call to endObject().  ​


​try​​ ​​{  ​


​stringer.object();  ​


​stringer.key(​​​​"languages"​​​​);  ​


​stringer.array();  ​


​//数组中的三个对象  ​


​stringer.object();  ​


​stringer.key(​​​​"id"​​​​).value(​​​​1​​​​).key(​​​​"ide"​​​​).value(​​​​"Eclispe"​​​​).key(​​​​"name"​​​​).value(​​​​"java"​​​​);  ​


​stringer.endObject();  ​


 


​stringer.object();  ​


​stringer.key(​​​​"id"​​​​).value(​​​​2​​​​).key(​​​​"ide"​​​​).value(​​​​"Xcode"​​​​).key(​​​​"name"​​​​).value(​​​​"Swift"​​​​);  ​


​stringer.endObject();  ​


 


​stringer.object();  ​


​stringer.key(​​​​"id"​​​​).value(​​​​3​​​​).key(​​​​"ide"​​​​).value(​​​​"Visual Studio"​​​​).key(​​​​"name"​​​​).value(​​​​"C++"​​​​);  ​


​stringer.endObject();  ​


​stringer.endArray();​​​​//数组结束  ​


 


​stringer.key(​​​​"cat"​​​​);  ​


​stringer.object();  ​


​stringer.key(​​​​"name"​​​​).value(​​​​"miao"​​​​).endObject(); ​​​​//结束object  ​


 


​stringer.endObject();  ​


 


​System.out.println(​​​​"json:"​​​​+stringer.toString());  ​


 


​} ​​​​catch​​ ​​(JSONException e) {  ​


​// TODO Auto-generated catch block  ​


​e.printStackTrace();  ​


​}  ​


​return​​ ​​stringer.toString();  ​


​}​



  Json数据的解析与生成还是很简单的,可以多去官网看看,多看看文档。。。android总结任重道远啊,写博文就是动力啊,坚持坚持。。。。。