原生态旅游网站开发需求分析,唐山做网站建设公司,网站是怎么优化的,怎么做网站卖产品Json:一种轻量级的数据交换格式#xff0c;具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案#xff08;有点类似于正则表达式 #xff0c;获得了当今大部分语言的支持#xff09;#xff0c;从而可以在不同平台间进行数据交换。JSON采用兼容性… Json:一种轻量级的数据交换格式具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案有点类似于正则表达式 获得了当今大部分语言的支持从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式同时也具备类似于C语言体系的行为。 – Json.org 官网地址http://www.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数组在方括号中对象在花括号中nullJSONObjectJSONArray 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 。 JSONStringerjson文本构建类 根据官方的解释这个类可以帮助快速和便捷的创建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。 JSONTokenerjson解析类 JSONExceptionjson中用到的异常 1.JSONObjectJSONArray解析创建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((linebr.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 i0;iarray.length();i){ JSONObject lan array.getJSONObject(i); System.out.println(..........); System.out.println(idlan.getInt(id)); System.out.println(idelan.getString(ide)); System.out.println(namelan.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总结任重道远啊写博文就是动力啊坚持坚持。。。。。 另附一篇很好的博文介绍了很多方法http://www.open-open.com/lib/view/open1326376799874.html 转载于:https://www.cnblogs.com/Free-Thinker/p/9708197.html