资源预览内容
第1页 / 共23页
第2页 / 共23页
第3页 / 共23页
第4页 / 共23页
第5页 / 共23页
第6页 / 共23页
第7页 / 共23页
第8页 / 共23页
第9页 / 共23页
第10页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
4 29 2020 JSON 讲解 李志敏 4 29 2020 概念 JSON JavaScriptObjectNotation isalightweightdata interchangeformat Itiseasyforhumanstoreadandwrite Itiseasyformachinestoparseandgenerate ItisbasedonasubsetoftheJavaScriptProgrammingLanguage StandardECMA 2623rdEdition December1999 JSONisatextformatthatiscompletelylanguageindependentbutusesconventionsthatarefamiliartoprogrammersoftheC familyoflanguages includingC C C Java JavaScript Perl Python andmanyothers ThesepropertiesmakeJSONanidealdata interchangelanguage 官网http www json org 4 29 2020 JSON书写格式 JSON的规则很简单 对象是一个无序的 名称 值 对 集合 一个对象以 左括号 开始 右括号 结束 每个 名称 后跟一个 冒号 名称 值 对 之间使用 逗号 分隔 规则如下 1 映射用冒号 表示 名称 值2 并列的数据之间用逗号 分隔 名称1 值1 名称2 值23 映射的集合 对象 用大括号 表示 名称1 值 名称2 值2 4 并列数据的集合 数组 用方括号 表示 名称1 值 名称2 值2 名称1 值 名称2 值2 5 元素值可具有的类型 string number object array true false null 4 29 2020 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度 4 29 2020 JSON数组 例1 name 张三 age 22 email zhangsan name 李四 age 23 email lisi name 王五 age 24 email wangwu 例2 student name 张三 age 22 email zhangsan name 李四 age 23 email lisi name 王五 age 24 email wangwu 4 29 2020 在javascript中使用json javascript解析json本身是不需要任何工具包的 也有一些封装好的更方便使用的工具包 下载地址 http www json org 例1 翻译json字符串为json对象并解析varpeopleStr firstName Brett lastName McLaughlin varpeople eval peopleStr 上面两行代码相当于 varpeople firstName Brett lastName McLaughlin alert people firstName alert people lastName 4 29 2020 在javascript中使用json 例2 解析json数组varpeople firstName Brett email brett newI firstName Mary email mary newI alert people 0 firstName alert people 0 email alert people 1 firstName alert people 1 email 4 29 2020 在javascript中使用json 例3 解析复杂json对象varpeople 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 address 0 city 4 29 2020 在java中使用json 在javase或者javaee中解析json格式的数据是需要导入jar包的 jar包有很多种可以到官网下载 具体使用自己研究 4 29 2020 在Android中使用json Android中内置了json的解析Api 4 29 2020 在Android中使用json JSONObject 4 29 2020 在Android中使用json JSONObject解析举例在服务器上有一个html文件InputStreamin conn getInputStream StringjsonStr DataUtil Stream2String in 将流转换成字符串的工具类2 通过构造函数将json字符串转换成json对象JSONObjectjsonObject newJSONObject jsonStr 3 从json对象中获取你所需要的键所对应的值JSONObjectjson jsonObject getJSONObject weatherinfo Stringcity json getString city Stringtemp json getString temp 4 29 2020 在Android中使用json JSONArray 4 29 2020 在Android中使用json JSONArray解析举例在服务器上有一个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 4 29 2020 在Android中使用json 解析步骤1 读取js文件源代码 获取一个json字符串StringjsonStr DataUtil Stream2String in 2 通过构造函数将json字符串转换成json数组JSONArrayarray newJSONArray jsonStr 3 遍历数组 获取数组中每一个json对象 同时可以获取json对象中键对应的值for inti 0 i array length i JSONObjectobj array getJSONObject i Stringtitle obj getString title Stringdescription obj getString description 注意 1 json数组并非全是由json对象组成的数组2 json数组中的每一个元素数据类型可以不相同Adenseindexedsequenceofvalues ValuesmaybeanymixofJSONObjects otherJSONArrays Strings Booleans Integers Longs Doubles nullorNULL ValuesmaynotbeNaNs infinities orofanytypenotlistedhere 如 94043 90210 或者 zhangsan 24 类似于javascript中的数组 4 29 2020 在Android中使用json 如何将一个json对象转换成json字符串 无论是json对象还是json数组都复写了toString方法 只需调用toString方法即可将json对象转换成一个格式良好的字符串 内部使用的是JSONStringer 4 29 2020 在Android中使用json 如何手动生成一个json对象 方法1 创建一个map 通过构造方法将map转换成json对象Mapmap newHashMap map put name zhangsan map put age 24 JSONObjectjson newJSONObject map 方法2 创建一个json对象 通过put方法添加数据JSONObjectjson newJSONObject json put name zhangsan json put age 24 4 29 2020 在Android中使用json 如何手动生成一个json数组 方法1 创建一个list 通过构造方法将list转换成json对象Mapmap1 newHashMap map1 put name zhangsan map1 put age 24 Mapmap2 newHashMap map2 put name lisi map2 put age 25 List list newArrayList list add map1 list add map2 JSONArrayarray newJSONArray list System out println array toString 4 29 2020 在Android中使用json 如何手动生成一个json数组 方法2 创建一个json数组对象 通过put的方式添加数据JSONArrayarray newJSONArray array put 0 zhangsan array put 1 lisi System out println array toString 打印结果 zhangsan lisi 4 29 2020 JSON与xml比较 1 json比xml文件更容易解析2 json数据占用空间大小通常要比xml小3 xml是文件级别 json是数据级别 可以存在于多种文件中 4 29 2020 JSON应用举例 腾讯开放平台获取用户信息 4 29 2020 JSON应用举例 公共服务信息如 国家气象局提供的天气预报接口接口地址 4 29 2020 頑張 友
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号