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);
  • 相关阅读:
    项目发展规划 题解
    善意的投票&小M的作物 题解
    方格取数加强版 题解
    BZOJ1001 狼抓兔子 题解
    a
    一个搬运
    代码“小白”的温故而知新(一)-----OA管理系统
    工作流-----WorkFlow
    温习SQL语句
    浅谈MVC基础
  • 原文地址:https://www.cnblogs.com/nadech/p/9268266.html
Copyright © 2011-2022 走看看