Android网络通讯——JSON,GSON解析网络数据
来源:互联网 发布:笨办法学python吧 编辑:程序博客网 时间:2024/05/16 03:37
1.将数据从xml或者json提取出来的技术 解析技术
xml
json
网络通信中,组织数据的格式。格式良好
网络通信中,组织数据的格式,格式良好
<name>汤</name>
{name:"汤"}
不常见
90%
数据量大-->流量消耗 大
小
dom4j,sax,pull
org.json 为android.jar自带 解析性能好
gson 也是google出的解析库 性能没有org.json
json性能好 开发速度慢(编写大量解析代码)
gson 性能不及json,开发速度快,只要一行代码
2. Json语法
一个变量
{name:"汤"}
变量:值
多个变量
{name:"汤",married:true}
用逗号隔开
一个对象
{name:"汤"}
以一个{}代表一个对象
多个对象
[{name:"汤",married:true},
{name:"刘",married:true},
{name:"李",married:true}
]
使用[]包裹,
多个对象使用逗号格开
4.5.1.案例1{name:"汤",married:true,age:33}{//jsonString json="{name:\"汤\",married:true,age:33}";//创建JSONObjectJSONObject obj=new JSONObject(json);//创建实体对象Person p=new Person();p.name=obj.getString("name");p.married=obj.getBoolean("married");p.age=obj.getInt("age");System.out.println(p);}
4.5.2.案例2//jsonString json="[{name:\"汤\",married:true,age:18},{name:\"刘\",married:true,age:18},{name:\"李\",married:true,age:18}]";ArrayList<Person> list=new ArrayList<Person>();//创建JSONArrayJSONArray array=new JSONArray(json);//循环for(int i=0;i<array.length();i++){//{name:\"汤\",married:true}JSONObject obj=array.getJSONObject(i);Person p=new Person();p.name=obj.getString("name");p.married=obj.getBoolean("married");p.age=obj.getInt("age");list.add(p);}System.out.println(list.size());
3. org.json
JSONObject
解析json对象
{name:"汤",married:true,age:33}
getString(变量名)
getBoolean(变量名)
getInt(变量名)
get类型(变量名)
JSONArray
解析json数组
[{name:"汤",married:true},
{name:"刘",married:true},
{name:"李",married:true}
]
获取数据
length()
get类型(下标)
getObject(0)
注意:数据实体类一定要生成一个无参构造方法
4.5.4 .gson案例@Testpublic void testGSONObject() throws JSONException {System.out.println("---testGson");// jsonString json = "{name:\"汤\",married:true,age:33}";// 创建GSON实例Gson gson = new Gson();// 转换Person p = gson.fromJson(json, Person.class);//System.out.println(p);}@Testpublic void testGsonArray() throws JSONException {String json="{list:[{name:\"汤\",married:true,age:18},{name:\"刘\",married:true,age:18},{name:\"李\",married:true,age:18}]}";//创建gsonGson gson=new Gson();PersonList list=gson.fromJson(json, PersonList.class);System.out.println(list);}
0 0
- Android网络通讯——JSON,GSON解析网络数据
- Android解析JSON数据(二)——GSON解析
- Android解析json数据(Gson)
- Android Gson解析JSON数据
- android Gson解析json数据
- 从零开始搞懂Android网络请求——JSON数据的解析与Gson框架的使用
- Android解析Json数据之Gson解析
- Android网络之数据解析----使用Google Gson解析Json数据
- Android网络之数据解析----使用Google Gson解析Json数据
- Android网络之数据解析----使用Google Gson解析Json数据
- Android入门——数据解析之使用GSON解析JSON字符串(二)
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android中使用Gson解析JSON数据
- Android下使用Gson解析JSON数据
- 再谈适配器模式(很多网上转载的图有问题,大家请注意)
- Ubuntu-proxy代理配置
- Java-数据库(SQL Server):-第四天-12/05/2014
- c++ list, vector, map, set 区别与用法比较
- URL中“#” “?” &“”号的作用以及js对URL信息的获取
- Android网络通讯——JSON,GSON解析网络数据
- 学习记录12-4
- html_learn
- JavaScript 00Introduction
- MyEclipse中如何改变.java文件和.html等网页文件的字体大小
- Google 地图原本为什么不采用矢量地图渲染,而是下载栅格化图像然后渲染?
- 串结构练习--字符串匹配
- TCP/IP协议族(一)
- 集群默认只使用每个节点8G内存的修改方法