JSON详解以及在android中的使用

上传人:油** 文档编号:123690594 上传时间:2022-07-23 格式:PPT 页数:24 大小:1.30MB
返回 下载 相关 举报
JSON详解以及在android中的使用_第1页
第1页 / 共24页
JSON详解以及在android中的使用_第2页
第2页 / 共24页
JSON详解以及在android中的使用_第3页
第3页 / 共24页
点击查看更多>>
资源描述
概念JSON(JavaScript Object Notation)is a lightweight data-interchange format.It is easy for humans to read and write.It is easy for machines to parse and generate.It is based on a subset of the JavaScript Programming Language,Standard ECMA-262 3rd Edition-December 1999.JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages,including C,C+,C#,Java,JavaScript,Perl,Python,and many others.These properties make JSON an ideal data-interchange language.官网http:/www.json.org/JSON书写格式JSON的规则很简单:对象是一个无序的“名称/值对”集合。一个对象以“”(左括号)开始,“”(右括号)结束。每个“名称”后跟一个“:”(冒号);“名称/值对”之间使用“,”(逗号)分隔。规则如下:1)映射用冒号(“:”)表示。名称:值2)并列的数据之间用逗号(“,”)分隔。名称1:值1,名称2:值23)映射的集合(对象)用大括号(“”)表示。名称1:值,名称2:值24)并列数据的集合(数组)用方括号(“”)表示。名称1:值,名称2:值2,名称1:值,名称2:值2 5)元素值可具有的类型:string,number,object,array,true,false,null JSON对象(键值对或键值对的集合)例1、name:Obama例2、name:Romney,age:56例3、city:name:bj,weatherinfo:weather:sunny例4、city:name:北京,“city_id”:101010100,weatherinfo:weather:sunny,temp:29度 JSON数组例1、name:张三,age:22,email:,name:李四,age:23,email:,name:王五,age:24,email: 例2、student:name:张三,age:22,email:,name:李四,age:23,email:,name:王五,age:24,email: 在javascript中使用jsonjavascript解析json本身是不需要任何工具包的,也有一些封装好的更方便使用的工具包(下载地址:http:/www.json.org)例1、翻译json字符串为json对象并解析 var peopleStr=firstName:Brett,lastName:McLaughlin;var people=eval(+peopleStr+);/上面两行代码相当于 /var people=firstName:Brett,lastName:McLaughlin;alert(people.firstName);alert(people.lastName);在javascript中使用json例2:解析json数组 var people=firstName:Brett,email:brettnewI,firstName:Mary,email:marynewI ;alert(people0.firstName);alert(people0.email);alert(people1.firstName);alert(people1.email);在javascript中使用json例3:解析复杂json对象 var people=username:mary,info:tel:1234566,celltelphone:788666,address:city:beijing,code:1000022,city:shanghai,code:2210444 ;alert(people.username);alert(people.info.tel);alert(people.address0.city);在java中使用json在javase 或者javaee中解析json格式的数据是需要导入jar包的,jar包有很多种可以到官网下载,具体使用自己研究 在Android中使用jsonAndroid中内置了json的解析Api 在Android中使用jsonJSONObject 在Android中使用jsonJSONObject解析举例在服务器上有一个html文件http:/ in=conn.getInputStream();String jsonStr=DataUtil.Stream2String(in);/将流转换成字符串的工具类2、通过构造函数将json字符串转换成json对象JSONObject jsonObject=new JSONObject(jsonStr);3、从json对象中获取你所需要的键所对应的值JSONObject json=jsonObject.getJSONObject(weatherinfo);String city=json.getString(city);String temp=json.getString(temp)在Android中使用jsonJSONArray 在Android中使用jsonJSONArray解析举例在服务器上有一个js文件http:/192.168.1.101/Web/news.js文件内容如下:title:国家发改委:台湾降油价和大陆没可比性,description:国家发改委副主任朱之鑫,image:http:/192.168.1.101/Web/img/a.jpg,comment:163,title:国家发改委:台湾降油价和大陆没可比性,description:国家发改委副主任朱之鑫,image:http:/192.168.1.101/Web/img/b.jpg,comment:0,title:国家发改委:台湾降油价和大陆没可比性,description:国家发改委副主任朱之鑫,image:http:/192.168.1.101/Web/img/c.jpg,comment:0;在Android中使用json解析步骤1、读取js文件源代码,获取一个json字符串String jsonStr=DataUtil.Stream2String(in);2、通过构造函数将json字符串转换成json数组JSONArray array=new JSONArray(jsonStr);3、遍历数组,获取数组中每一个json对象,同时可以获取json对象中键对应的值for(int i=0;i array.length();i+)JSONObject obj=array.getJSONObject(i);String title=obj.getString(title);String description=obj.getString(description);注意:1、json数组并非全是由json对象组成的数组 2、json数组中的每一个元素数据类型可以不相同A dense indexed sequence of values.Values may be any mix of JSONObjects,other JSONArrays,Strings,Booleans,Integers,Longs,Doubles,null or NULL.Values may not be NaNs,infinities,or of any type not listed here.如:94043,90210或者zhangsan,24类似于javascript中的数组 在Android中使用json如何将一个json对象转换成json字符串?无论是json对象还是json数组都复写了toString方法,只需调用toString方法即可将json对象转换成一个格式良好的字符串(内部使用的是JSONStringer)在Android中使用json如何手动生成一个json对象?方法1、创建一个map,通过构造方法将map转换成json对象Map map=new HashMap();map.put(name,zhangsan);map.put(age,24);JSONObject json=new JSONObject(map);方法2、创建一个json对象,通过put方法添加数据JSONObject json=new JSONObject();json.put(name,zhangsan);json.put(age,24);在Android中使用json如何手动生成一个json数组?方法1、创建一个list,通过构造方法将list转换成json对象Map map1=new HashMap();map1.put(name,zhangsan);map1.put(age,24);Map map2=new HashMap();map2.put(name,lisi);map2.put(age,25);ListMap list=new ArrayListMap();list.add(map1);list.add(map2);JSONArray array=new JSONArray(list);System.out.println(array.toString();在Android中使用json如何手动生成一个json数组?方法2、创建一个json数组对象,通过put的方式添加数据JSONArray array=new JSONArray();array.put(0,zhangsan);array.put(1,lisi);System.out.println(array.toString();打印结果:zhangsan,lisi JSON与xml比较1、json比xml文件更容易解析2、json数据占用空间大小通常要比xml小3、xml是文件级别,json是数据级别,可以存在于多种文件中 JSON应用举例腾讯开放平台获取用户信息:http:/ JSON应用举例公共服务信息如:国家气象局提供的天气预报接口接口地址:http:/ 頑張 友24 结束语结束语
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!