在上一篇文章中介绍了如何从服务器端获取数据,对于服务器来说,返回的数据一般包括html、json和xml,在本篇中我们将介绍怎么处理服务器返回的json格式的数据。
在android的org.json包中已经包含了解析json格式的一些api,所以在android环境下,不需要导入其他的jar包就直接可以解析json数据。在此处将不具体对json作详述,但在解析之前还是有必要验证下json是否规范。所谓的解析json无非就是将json数据转化为一个实体类,所以在此先创建一个实体类。
/**
* 部门信息实体类
*/
class DepartmentInfo
{
private String depName;// 部门名字
private int total;// 员工总数
private List<Employee> empList; //员工
public String getDepName()
{
return depName;
}
public void setDepName(String depName)
{
this.depName = depName;
}
public int getTotal()
{
return total;
}
public void setTotal(int total)
{
this.total = total;
}
public List<Employee> getEmpList()
{
return empList;
}
public void setEmpList(List<Employee> empList)
{
this.empList = empList;
}
@Override
public String toString()
{
return "DepartmentInfo [depName=" + depName + ", total=" + total
+ ", empList=" + empList + "]";
}
}
/**
* 员工信息
*/
class Employee
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", age=" + age + "]";
}
}
先看看要解析的json的内容:
{
"department": "研发",
"total": 30,
"employeeList": [
{
"name": "张三",
"age": 24
},
{
"name": "李四",
"age": 25
}
]
}
上面的DepartmentInfo和Employee分别描述部门的信息和员工信息,我们对应这个json数据来看看解析json数据的代码:
/**
* android 解析Json
*/
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonUtil
{
public static DepartmentInfo parseJson(String json)
{
DepartmentInfo depInfo = new DepartmentInfo();
List<Employee> empList = new ArrayList<Employee>();
try
{
//将json格式的字符串转化为一个JSONObject对象
JSONObject jsonObj = new JSONObject(json);
//获得key值为department的值,在取值是应根据值的类型
//json提供的类型有整型(int),字符串(String),浮点型(double),长整型(long),布尔型(boolean)
//特殊的还有数组,通常是由"[","]"括起来的部分
depInfo.setDepName(jsonObj.getString("department"));
depInfo.setTotal(jsonObj.getInt("total"));
//获取员工列表,返回一个JSONArray对象
JSONArray array = jsonObj.getJSONArray("employeeList");
for (int i = 0; i < array.length(); i++)
{
//JSONArray 其实等价于 ArrayList<JSONObject>
JSONObject arr_jsonObj = array.getJSONObject(i);
Employee emp = new Employee();
emp.setName(arr_jsonObj.getString("name"));
emp.setAge(arr_jsonObj.getInt("age"));
//保存员工信息
empList.add(emp);
}
//保存部门信息
depInfo.setEmpList(empList);
} catch (JSONException e)
{
e.printStackTrace();
}
return depInfo;
}
}
通过以上方法即将json格式转化为一个实体对象,我们将该对象打印出来验证一下。
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
private DepartmentInfo depInfo;
//此处的json数据时我们的一个示例,在开发中json数据通常是从服务器端发送过来的
String jsonString = "{\"department\": \"研发\"," + "\"total\": 30,"
+ "\"employeeList\": [" + "{\"name\": \"张三\",\"age\": 24},"
+ "{\"name\": \"李四\",\"age\": 25}]}";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//解析json数据
depInfo = JsonUtil.parseJson(jsonString);
System.out.println(depInfo.toString());
}
}
打印结果:
小小的实例,是不是觉得android解析json很简单呢……