• 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);
  • 相关阅读:
    Java SE (3) 之 事件监听
    Java SE (2)之 Graphics 画图工具
    Java SE (1)之 JFrame 组件 GridLayout布局
    解决IIS7中出现An error occurred on the server when processing the URL错误提示的方法
    VS2010中配置C#Project不生成.vhost.exe和.pdb文件的方法
    IIS7和IIS7.5备份和还原的方法
    C# 读取IE缓存文件(2)
    C# 读取IE缓存文件(1)
    raywenderlich的Swift编程风格指南
    Swift和C#的基本语法对比
  • 原文地址:https://www.cnblogs.com/nadech/p/9268266.html
Copyright © 2020-2023  润新知