Fastjson解析json数据


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"));
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM