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"));
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。