import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class Demo { public static void main(String[] args) { String data = "{\"name\":\"zhangli\",\"age\":22,\"sex\":\"女\",\"skill\":[\"java\",\"python\",\"JavaScript\"],\"depts\":[{\"deptno\":11,\"dname\":\"Accounting\",\"loc\":\"中国\"},{\"deptno\":22,\"dname\":\"Maneager\",\"loc\":\"上海\"}]}"; // String转为json对象 JSONObject jsonObject = JSONObject.parseObject(data); //读取name String name = jsonObject.getString("name"); System.out.println("name: " + name); //取得skill数组 JSONArray skill = jsonObject.getJSONArray("skill"); for (int i = 0; i < skill.size(); i++) { System.out.println(skill.get(i)); } //depts json数组 JSONArray depts = jsonObject.getJSONArray("depts"); for (int i = 0; i < depts.size(); i++) { JSONObject temdpts = depts.getJSONObject(i); System.out.println("部门编号: " + temdpts.getString("deptno")); System.out.println("部门名称: " + temdpts.getString("dname")); System.out.println("部门位置: " + temdpts.getString("loc")); } } }