1、json格式
2、json解析 3、gson解析 4、fastjson解析 一、Json格式 json一种轻量级的数据交换格式。在网络上传输交换数据一般用xml, json。 两种结构: 1)对象(key-value集合) {"id":"1001","name":"zhangshan","address":"shanghai"} 2)数组(数组对象),以[ ] 包括的key-value对象{"students":[{"sid":1001,"name":"zhangsan","addr":"Yichang","pic":"111.jpg"}, {"sid":1002,"name":"Jack","addr":"Beijing","pic":"222.jpg"}]}
或:
{[{"sid":1001,"name":"zhangsan","addr":"Yichang","pic":"111.jpg"}, {"sid":1002,"name":"Jack","addr":"Beijing","pic":"222.jpg"}]}
二、json数据生成,解析 JSONObject,代表了一个JSON对象,可以完成Java对象与JSON字符串之间的相互转换。 JSONArray,代表一个JSON数组,可以完成Java集合(集合元素可以是对象)与JSON字符串之间的转换。 1、Java对象与JSON字符串相互转换 方式一:Student stu = new Student(1005,"张三","Wuhan","zs.jpg");
JSONObject jsonObject = new JSONObject(stu);
jsonObject.toString()就是字符串输出: {"sid":1005,"name":"张三","addr":"Wuhan","pic":"zs.jpg"} 解析为对象:JSONObject jsonObject = new JSONObject(str);
Iterator<String> iterator = jsonObject.keys();
Student stu = new Student();
while(iterator.hasNext()){
//可以利用java反射,写泛型方法
String key = iterator.next();
if(key.equals("sid")){
stu.setSid(jsonObject.getInt(key));
}
if(key.equals("name")){
stu.setName(jsonObject.getString(key));
}
if(key.equals("sid")){
stu.setAddr(jsonObject.getString(key));
}
if(key.equals("sid")){
stu.setPic(jsonObject.getString(key));
}
}
方式二: Student stu = new Student(1005, "张三", "Wuhan", "zs.jpg");JSONObject jsonObject = new JSONObject();
jsonObject.put("zs", stu);
同样jsonObject.toString()就是字符串输出 {"zs":"{sid:1005, name:张三, addr:Wuhan, pic:zs.jpg}"}。要注意,如果Student没有覆写toString()方法,输入的是:{"zs":"org.itair.domain.Student@2666e815"}
2、集合与JSON字符串相互转换 方式一、 转换:
Student s1 = new Student(1001,"zhangsan","yichang","111");
Student s2 = new Student(1002,"Jack","Beijing","222");
List<Student> stus = new ArrayList<Student>();
stus.add(s1);
stus.add(s2);
JSONArray array = new JSONArray(stus);
array的字符串是:[{"sid":1001,"name":"zhangsan","addr":"yichang","pic":"111"},{"sid":1002,"name":"Jack","addr":"Beijing","pic":"222"}]
解析: JSONArray jsonArray = new JSONArray("要解析的json字符串");for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
//解析成特定的对象操作,new对象,从jsonObject2取出值来装进去
Iterator<String> iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject2.get(key);
map.put(key, value);
}
}
方式二、 转换: Student s1 = new Student(1001,"zhangsan","yichang","111");Student s2 = new Student(1002,"Jack","Beijing","222");
List<Student> stus = new ArrayList<Student>();
stus.add(s1);
stus.add(s2);
JSONObject jsonObject = new JSONObject();
jsonObject.put("students", stus);
jsonObject的字符串:{"students":[{"sid":1001,"name":"zhangsan","addr":"yichang","pic":"111"},{"sid":1002,"name":"Jack","addr":"Beijing","pic":"222"}]}
解析: 由于上面的转换方式是把集合对象放到 JSONObject中。 JSONObject jsonObject = new JSONObject("要解析的json字符串");JSONArray jsonArray = jsonObject.getJSONArray("key");//key就是上面放入的students
后面的代码同上循环。
在Android的项目中,一般不会把Json字符串解析成对象的集合,而多是解析成List<Map<String,Object>>
List<Map<String,Object>> list = new ArrayList<Map<String ,Object>>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Map<String ,Object> map = new HashMap<String, Object>();
Iterator<String> iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject2.get(key);
map.put(key, value);
}
list.add(map);
}
要解析成对象集合,还是用Gson, fastjson比较方便。
三、Gson toJson(Object) 把对象转为json字符串 fromJson(String, Object) 把json字符串转为对象