zoukankan      html  css  js  c++  java
  • 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"));
            }
        }
    }

  • 相关阅读:
    原型模式(8)
    工厂方法模式(7)
    代理模式(6)
    装饰模式(5)
    策略模式与简单工厂结合(4)
    策略模式(3)
    简单工厂模式(2)
    序(1)
    国际控制报文协议ICMP
    IP 转发分组的流程
  • 原文地址:https://www.cnblogs.com/wakey/p/13677255.html
Copyright © 2011-2022 走看看