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