zoukankan      html  css  js  c++  java
  • FastJson

    FastJson使用


    比较关注的是JSON对象和JSON字符串之间的转换

    1.FastJson特点

    1. FastJson数度快,无论序列化和反序列化,都是当之无愧的fast
    2. 功能强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)
    3. 零依赖(没有依赖其它任何类库)

    2.FastJson的三个类

    1. JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换
    2. JSONObject:fastJson提供的json对象
    3. JSONArray:fastJson提供json数组对象

    3.JSON格式字符串和JSON对象之间转换

    //json字符串-简单对象型
    private static final String  JSON_OBJ_STR = "{"studentName":"lily","studentAge":12}";
    
    //json字符串-数组类型
    private static final String  JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]";
    
    //复杂格式json字符串
    private static final String  COMPLEX_JSON_STR = "{"teacherName":"crystall","teacherAge":27,"course":{"courseName":"english","code":1270},"students":[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]}";
    

    3.0统一通用的相互转换

    //记得这两条代码,可以不管一下的分类转换
    JSONObject jsonObject = JSON.parseObject(JSON_STR);
    Strign jsonStr = JSON.toJSONString(jo);
    

    3.1 json字符串-简单对象型与JSONObject之间的转换

    JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
    String jsonString = JSONObject.toJSONString(jsonObject);
    

    3.2 json字符串(数组类型)与JSONArray之间的转换

    JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
    String jsonString = JSONArray.toJSONString(jsonArray);
    

    3.3 复杂json格式字符串与JSONObject之间的转换

    JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
    String jsonString = JSONObject.toJSONString(jsonObject);
  • 相关阅读:
    面试题15 链表中倒数第k个结点
    面试题14 调整数组顺序使奇数位于偶数前面
    面试题13 在O(1)时间删除链表结点
    面试题12 打印1到最大的N位数
    面试题11 数值的整数次方
    面试题10 二进制中1的个数
    面试题9 斐波那契数列
    面试题8 旋转数组的最小数字
    关于神经网络训练的一些建议笔记
    两篇将rf和boosting方法用在搜索排序上的paper
  • 原文地址:https://www.cnblogs.com/nadech/p/9268266.html
Copyright © 2011-2022 走看看