描述:fastjson是目前java语言中最快的json库,比自称最快的jackson速度要快,第三方独立测试结果看这 里:https://github.com/eishay/jvm-serializers/wiki/Staging-Results。
使用方式:
比如解析如下格式的数据:
{"id":1,"name":"user","result":[{"id":2,"name":"child1"},{"id":3,"name":"child2"}]}
1、代码:
Group g = JSON.parseObject(str,Group.class); //str 要解析字符串 String n = g.getName(); List<User> us = g.getUserList(); for (User u : us) { android.util.Log.d("mark","u=" + u.getName()); } android.util.Log.d("mark","n=" + n);
package com.example.domain; import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.annotation.JSONField; public class Group { private int id; private String name; private List<User> usersList = new ArrayList<User>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<User> getUserList() { return usersList; } @JSONField(name = "result") //使用注解可以方便的处理服务器传过来的结果 public void setUserList(List<User> usersList) { this.usersList = usersList; } }
package com.example.domain; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2、不要忘记加入相应的fastjson.jar 包 jar 包下载 点击打开链接