• FastJson


    FastJson的应用

    一、阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
    速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson;
    功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖,能够直接运行在Java SE 5.0以上版本;支持Android;开源 (Apache 2.0)

    将javaBean对象包装成json格式

                    JSONObject jsonObject=new JSONObject();
            User user=new User("尚晓飞", "男", 25, new Date(), "bjsxt");
            jsonObject.put("user", user);
            System.out.println("Test.main()"+jsonObject.toString());
    
                    //打印结果:Test.main(){"user":{"age":25,"birthDay":1405067572140,"name":"尚晓飞","school":"bjsxt","sex":"男"}}
    View Code

    将list集合包装成json

     1 User user=new User("尚晓飞", "男", 25, new Date(), "bjsxt");
     2         User user2=new User("sxf", "女", 22, new Date(), "北京尚学堂");
     3         User user3=new User("当当", "男", 25, new Date(), "当当网");
     4         List<Object> userList=new ArrayList<Object>();
     5         
     6         userList.add(user);
     7         userList.add(user2);
     8         userList.add(user3);
     9         
    10         JSONObject jsonObject=new JSONObject();
    11         jsonObject.put("userlist", userList);
    12         
    13         
    14         System.out.println("Test.main()"+jsonObject);
    15         
    16 //打印结果:Test.main(){"userlist":[{"age":25,"birthDay":1405069939080,"name":"尚晓飞","school":"bjsxt","sex":"男"},{"age":22,"birthDay":1405069939080,"name":"sxf","school":"北京尚学堂","sex":"女"},{"age":25,"birthDay":1405069939080,"name":"当当","school":"当当网","sex":"男"}]}
    View Code

    将list集合包装成json格式的数组

     1 User user=new User("尚晓飞", "男", 25, new Date(), "bjsxt");
     2         User user2=new User("sxf", "女", 22, new Date(), "北京尚学堂");
     3         User user3=new User("当当", "男", 25, new Date(), "当当网");
     4         List<Object> userList=new ArrayList<Object>();
     5         
     6         userList.add(user);
     7         userList.add(user2);
     8         userList.add(user3);
     9         
    10         JSONArray jsonArray=new JSONArray(userList);
    11         
    12         
    13         System.out.println("Test.main()"+jsonArray);
    14 
    15 //打印结果:Test.main()[{"age":25,"birthDay":1405069524031,"name":"尚晓飞","school":"bjsxt","sex":"男"},{"age":22,"birthDay":1405069524031,"name":"sxf","school":"北京尚学堂","sex":"女"},{"age":25,"birthDay":1405069524031,"name":"当当","school":"当当网","sex":"男"}]
    View Code

    将list集合包装成json格式的数组

    1 User user=new User("尚晓飞", "男", 25, new Date(), "bjsxt");
    2         User user2=new User("sxf", "女", 22, new Date(), "北京尚学堂");
    3         User user3=new User("当当", "男", 25, new Date(), "当当网");
    4         List<Object> userList=new ArrayList<Object>();
    5         
    6         userList.add(user);
    7         userList.add(user2);
    8         userList.add(user3);
    9         JSON json=(JSON) JSON.toJSON(userList);
    View Code

      在javascript中操作json对象

     1 //如果是一个json对象var abc={"key":"value"}
     2 //取出key对应的值
     3 var v=abc.key
     4 var v=abc["key"]
     5 
     6 
     7 //如果是一个json数组的对象   var abc=[{"one":"一","name":"尚晓飞"},{"one":"二","name":"尚小帅"}]
     8 
     9 for(var i=0;i<abc.length;i++){
    10      var m=abc[i].name;
    11      var o=abc[i].one; 
    12      var m=abc[i]["name"]           
    13 }
    View Code

     FastJSON的API

    1 public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray 
    2 public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject    
    3 public static final  T parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean 
    4 public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray 
    5 public static final  List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 
    6 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 
    7 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 
    8 public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
    View Code

    测试代码

     1 Fave fave1=new Fave("打游戏", new Date());
     2         Fave fave2=new Fave("下象棋", new Date());
     3         
     4         Student st1=new Student("尚晓飞", 1,new Date(),fave1);
     5         Student st2=new Student("尚小帅",1,new Date(),fave2);
     6         
     7         List<Student> list=new ArrayList<Student>();
     8         list.add(st1);
     9         list.add(st2);
    10         
    11         //javaBean---》json格式字符串
    12         String jsonString=JSONObject.toJSONString(st1);
    13         String listString=JSONObject.toJSONString(list);
    14         System.out.println("javabean--->string"+jsonString);
    15         //打印结果:javabean--->string{"birthday":1411713587089,"fave":{"date":1411713587088,"name":"打游戏"},"name":"尚晓飞","sex":1}
    16         System.out.println("list---->String"+listString);
    17         //打印结果:list---->String[{"birthday":1411714397844,"fave":{"date":1411714397843,"name":"打游戏"},"name":"尚晓飞","sex":1},
    18         //{"birthday":1411714397844,"fave":{"date":1411714397843,"name":"下象棋"},"name":"尚小帅","sex":1}]
    19         
    20         //javaBean---》json对象
    21         JSONObject jsonObject=(JSONObject) JSONObject.toJSON(st1);
    22         
    23         String nameString=(String) jsonObject.get("name");//根据key值获取json对象的value值
    24         Object object= jsonObject.get("fave");//特殊:转成的fave内容为{key:vlaue}所以说不能够转换成fave对象,只能进行二次转换,转换成javaBean
    25         String fav=object.toString();
    26         
    27         //将json格式的字符串转换成---》javaBean
    28         Fave fave=JSONObject.parseObject(fav, Fave.class);
    29         System.out.println("Test.main()"+nameString);//尚晓飞
    30         System.out.println("Test.main()"+fave.getName());//打游戏
    View Code
  • 相关阅读:
    服务管理器
    自动启动管理器
    进程管理器
    进程模块查看器
    无DLL远程注入
    远程DLL注入
    U盘免疫
    WSAAsyncSelect Demo
    select Demo
    校正系统时间
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/3838098.html
Copyright © 2020-2023  润新知