标签:json
/**
* 解析Json数据
*
* @param jsonString Json数据字符串 */
public static void ParseJson(String jsonString) { // 以employee为例解析,map类似
JSONObject jb = JSONObject.fromObject(jsonString);
JSONArray ja = jb.getJSONArray("employee");
List<Employee> empList = new ArrayList<Employee>(); // 循环添加Employee对象(可能有多个)
for (int i = 0; i < ja.size(); i++) {
Employee employee = new Employee();
employee.setName(ja.getJSONObject(i).getString("name"));
employee.setSex(ja.getJSONObject(i).getString("sex"));
employee.setAge(ja.getJSONObject(i).getInt("age"));
empList.add(employee);
}
System.out.println("\n将Json数据转换为Employee对象:");
for (int i = 0; i < empList.size(); i++) {
Employee emp = empList.get(i);
System.out.println("name: " + emp.getName() + " sex: "
+ emp.getSex() + " age: " + emp.getAge());
}
}
本文出自 “秦枫” 博客,请务必保留此出处http://leoxia.blog.51cto.com/10825018/1732137
标签:json
原文地址:http://leoxia.blog.51cto.com/10825018/1732137