记android学习之路----android中对json数据的解析
json:
1:json是javascript对象;
2:是一种数据格式;
3:用于传递数据
4:json非常轻量级,传输速度非常快;在接口数据传递中应用非常广泛;
5:android中对json的解析和封装是通过JSONObject和JSONArray来进行的;
JSONObject:
用于封装或者处理json数据的;
1:创建实例:
//创建一个空的JSONOBject实例,一般用于封装json数据
JSONOBject jsonObj = new JSONOBject();
//通过一个json字符串创建JSONOBject实例,一般用于解析json数据
JSONOBject jsonObj = new JSONOBject(String json);
JSONOBject jsonObj = new JSONOBject(Map copyFrom);
2:常用方法:
封装:
put(“key”,value);
//value是基本数据类型;
获取:
getXxx(“key”);
//Xxx是基本数据类型
getJSONArray();
getJSONObject();
判断:
has(“key”);
isNull();
length();
3:JSONArray:
用于操作json字符串中的数组:
基本形态:
“{[“name”,”age”,”password”]}”
“[“name”,”age”,”password”]”
“[{“name”:”tom”,”age”:20,”password”:1234}]”
1:创建实例:
//创建一个空的JSONArray实例,一般用于封装json数组
JSONArray jsonArr = new JSONArray();
//通过一个json字符串创建JSONArray实例,一般用于解析json数组
JSONArray jsonArr = new JSONArray(String json);
JSONArray jsonArr = new JSONArray(array arr);2:常用方法:封装: put(value); //value可以是基本数据类型;获取: getXxx(int index); //Xxx是基本数据类型 getJSONArray(int index); getJSONObject(int index); get(int index)判断: isNull(); length();
2:封装数据:
(1):
String jsonStr = {“name”:”tom”,”password”:123456}
JSONObject jsonStr = new JSONObject();jsonStr.put("name","tom");jsonStr.put("password",123456);
(2):
String jsonStr = {“name”:”tom”,”password”:123456,”group”:[“lili”,”join”]}
JSONArray jsonArr = new JSONArray();jsonArr.put("lili");jsonArr.put(join);//或者//String[] jsonArray = new String[]("lili","join");// JSONArray jsonArr = new JSONArray(jsonArray);JSONObject jsonStr = new JSONObject();jsonStr.put("name","tom");jsonStr.put("password",123456);jsonObj.put("group",jsonArr);
3:解析数据:
1:jsobj = {“name”:”tom”,”password”:123456}
JSONObject jsonObj = new JSONObject(jsobj);
jsonobj.getString(0);
jsonobj.getInt(1);
2:jsonStr = {"name":"tom","password":123456,"group":["lili","join"]}JSONObject jsonObj = new JSONObject(jsonStr);JSONArray jsarr = jsonObj.getJSONArray(group);jsarr.getString(0);jsarr.getString(1);3:jsonStr = {"name":"tom","password":123456,"group":[{"name":"lili","age":20}]}JSONObject jsonObj = new JSONObject(jsonStr);JSONArray jsarr = jsonObj.getJSONArray(group);JSONObject jsonObj2 = jsarr.getJSONObject(0);String name1 = jsonObj2.getString("name");int age = jsonObj2.getInt("age");
Gson:
1:下载Gson的jar包;
2:将其解压后放入libs目录;
3:Gson是google提供的一个解析和生成json的包;
4:作用原理就是:将json对象映射成java对象;
Gson:
1:创建Gson对象用于解析或者生成json:
1:Gson json = new Gson();
2:通过GsonBuilder 可以配置多种选项:
gson = new GsonBuilder()
.setLenient()// json宽松
.enableComplexMapKeySerialization()//支持Map的key为复杂对象的形式
.serializeNulls() //智能null
.setPrettyPrinting()// 调教格式
.disableHtmlEscaping() //默认是GSON把HTML 转义的
.create();
2:Gson中常用的方法: Gson.toJson(Object); Gson.fromJson(Reader,Class); Gson.fromJson(String,Class); Gson.fromJson(Reader,Type); Gson.fromJson(String,Type);3:常见应用: 生成json串: 1:将对象转为json串: class User{ public String name; public int age; public String home; public user(na,ag,ho){ name = na; age = ag; home = ho; } } Gson json = new Gson(); User user = new User("tom",120,"shenzhen"); json.toJson(user); //{"name":"tom","age":120,"home":"shenzhen"} 2:将List转为json字符串: List list = new List; list.add("tom"); list.add("lili"); json.toJson(list); //["tom","lili"] 3:将Map转换为json子串: Map content = new HashMap(); content.put("name", "xuanyouwu"); content.put("age", "26"); gson.toJson(content): //{"name":"xuanyouwu","age":"26"} 解析json子串: 1:json子串转换为java对象: jsonStr = {"name":"xuanyouwu","age":26}; public class User{ public String name; public int age; } User student1 = gson.fromJson(jsonStr, User.class); 2:将json转换为List; jsonStr = ["tom","lili","jone"]; Type type = new TypeToken>(){}.getType(); ArrayList sList=gson.fromJson(listJsonStr, type); 3:
解析json数据:
1:jsonData = {“name”:”tom”,”age”:22,”home”:”shenzhen”}
1:创建一个类包含一些属性,属性名称和json数据的项名称一致:如:2:创建Gson对象: Gson json = new Gson(); User user = json.fromJson(jsonData,User.class);3:获取json数据: user.name:"tom"; user.age:22; user.home:"shenzhen";
2:生成上述数据:
2:jsonData = {[“tom”,”lili”,”jone”]};
1:创建一个类包含一些属性,属性名称和json数据的项名称一致:
如:
class User{
public String name;
public int age;
public String home;
}
2:创建Gson对象:
Gson json = new Gson();
User user = json.fromJson(jsonData,new TypeToken
更多相关文章
- android中的search dialog
- Android中的单任务模式
- Android(安卓)binder学习一:主要概念
- android sqlite批量插入数据速度慢解决方案
- AIDL的实例,看一看结构
- android数据库操作出现的 android.database.sqlite.SQLiteConstr
- 剖析andriod联系人
- Android本地动作
- LoganSquare——快速解析和序列化JSON对象的Android类库