在开发Android的过程中,如果我们经常与服务器打交道,更新数据等等,那么json必然是个好的数据格式,但是有了json我们要解析它,使用原生的解析也可以,但是很不高效,所以这里介绍两种json数据解析的方式,一种是FastJSon ,这个是阿里巴巴出的,号称最快的解析速度。第二种使我们伟大的Google提供的Gson 来解析json,两个解析json都很方便,而且都很强大,在我使用中,基本上都满足了我的需求,下面简单就接受一下,我们的使用方法。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.23</version>
</dependency>
User.java
package vo;
import java.util.Date;
public class User {
private String userName ;
private String userPass ;
private String sex ;
private int age ;
private Date birthDay ;
public User() {}
public User(String userName, String userPass, String sex, int age,
Date birthDay) {
super();
this.userName = userName;
this.userPass = userPass;
this.sex = sex;
this.age = age;
this.birthDay = birthDay;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Override
public String toString() {
return "User [userName=" + userName + ", userPass=" + userPass
+ ", sex=" + sex + ", age=" + age + ", birthDay="
+ birthDay + "]";
}
}
FastJsonTest.java
package gsonTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import vo.User;
public class FastJsonTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 1 test object , create Gson
User user = new User("hh", "123", "火影忍者", 20, new Date());
String str = JSON.toJSONString(user);
System.out.println(str);
// -1 test gson ,create object
User u = (User) JSON.parseObject(str, User.class);
System.out.println(u.getUserName());
// -1 test gson ,create object
User u2 = (User) JSON.parseObject(str, new TypeReference<User>(){});
System.out.println(u2.getUserName());
// 2 test List ,create Gson
List<User> listUser = new ArrayList<User>();
listUser.add(new User("rr1", "123", "火影", 25, new Date()));
listUser.add(new User("rr2", "123", "澳门", 25, new Date()));
listUser.add(new User("rr3", "123", "海岸", 25, new Date()));
String listStr = JSON.toJSONString(listUser);
System.out.println(listStr);
// -2 test String ,create list
@SuppressWarnings("unchecked")
List<User> list2 = (List<User>) JSON.parseObject(listStr,
new TypeReference<List<User>>(){});
for (User ux : list2) {
System.out.println(ux.getUserName());
}
// 3 test Nested list<list> ,create Gson
List<List<User>> list = new ArrayList<List<User>>();
List<User> userList = new ArrayList<User>();
userList.addAll(listUser);
list.add(listUser);
list.add(userList);
String list2Str = JSON.toJSONString(list);
System.out.println(list2Str);
// -3 test String ,create list<list>
List<List<User>> llu = new ArrayList<List<User>>();
llu = (List<List<User>>) JSON.parseObject(list2Str,
new TypeReference<List<List<User>>>(){});
for (List<User> lu : llu) {
for (User u1 : lu) {
System.out.println(u1.getUserName());
}
}
// 4 test Map ,create Gson ;
Map<String, String> gmap = new HashMap<String, String>();
gmap.put("111", "222");
gmap.put("222", "333");
gmap.put("444", "555");
String strgmap = JSON.toJSONString(gmap);
System.out.println(strgmap);
// -4 test String ,create Map
Map<String, String> map2 = new HashMap<String, String>();
map2 = (Map<String, String>)JSON.parseObject(strgmap,
new TypeReference<Map<String, String>>(){});
Iterator<String> it = map2.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + "==" + map2.get(key));
}
// 5 test map<String,object>
Map<String, Object> objmap = new HashMap<String, Object>();
objmap.put("111", new User("rr1", "123", "哈哈", 25, new Date()));
objmap.put("222", new User("rr2", "123", "呵呵", 25, new Date()));
objmap.put("333", new User("rr3", "123", "哒哒", 25, new Date()));
String mapobj = JSON.toJSONString(objmap);
System.out.println(mapobj);
// -5 test String ,create Map
Map<String, User> map3 = new HashMap<String, User>();
map3 = JSON.parseObject(mapobj, new TypeReference<Map<String, User>>(){});
Iterator<String> it2 = map3.keySet().iterator();
while (it2.hasNext()) {
String key = it2.next();
System.out.println(key + "==" + map3.get(key));
}
// 6 test Nested list<Map> ,create Gson
List<Map<String, String>> map = new ArrayList<Map<String, String>>();
Map<String, String> mm = new HashMap<String, String>();
mm.put("111", "222");
mm.put("222", "333");
mm.put("444", "555");
Map<String, String> mm2 = new HashMap<String, String>();
mm2.put("111", "222");
Map<String, String> mm3 = new HashMap<String, String>();
mm3.put("111", "222");
map.add(mm);
map.add(mm2);
map.add(mm3);
String strMap = JSON.toJSONString(map);
System.out.println(strMap);
// -6
List<Map<String, String>> map4 = new ArrayList<Map<String, String>>();
map4 = (List<Map<String, String>>) JSON.parseObject(strMap,
new TypeReference<List<Map<String, String>>>(){});
for(Map m:map4){
System.out.println(m);
Iterator<String> it3 = m.keySet().iterator();
while (it3.hasNext()) {
String key = it3.next();
System.out.println(key + "==" + m.get(key));
}
}
}
}