zoukankan      html  css  js  c++  java
  • java中JSON转换

    1.JSON介绍

    JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。

    JSON就是一串字符串 只不过元素会使用特定的符号标注。

    {} 双括号表示对象

    [] 中括号表示数组

    "" 双引号内是属性或值

    : 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)

    所以 {"name": "Michael"} 可以理解为是一个包含name为Michael的对象

    而[{"name": "Michael"},{"name": "Jerry"}]就表示包含两个对象的数组

    当然了,你也可以使用{"name":["Michael","Jerry"]}来简化上面一部,这是一个拥有一个name数组的对象

    2. json-lib  SUN公司

    (不推荐使用)

    依赖导入6个jar包:

    commons-beanutils.jar 

    commons-collections.jar 

    commons-lang.jar 

    commons-logging.jar  

    ezmorph.jar 

    json-lib.jar

    对象:

    JSONObject和JSONArray

    创建:

            //创建一个JSONObject对象  
            JSONObject jsonObject = new JSONObject();  
            jsonObject.put("key1", "value1");  
            jsonObject.put("key2", "value2");  
            jsonObject.put("key3", "value3");  
            jsonObject.put("key4", "value4");  
            System.out.println("jsonObject:"+jsonObject);  
              
            //创建一个JSONArray对象  
            JSONArray jsonArray=new JSONArray();  
            jsonArray.add("value1");  
            jsonArray.add("value2");  
            jsonArray.add("value3");  
            jsonArray.add("value4");  
            System.out.println("jsonArray:"+jsonArray);  

    结果:

    jsonObject:{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}
    jsonArray:["value1","value2","value3","value4"]

    转换:

    JSONObject JSONObject.fromObject(javaBean对象或Map集合)
    JSONArray JSONArray.fromObject(数组或List集合)

    注意JsonArray解析map,会将整个json对象放进数组。  

          例如:JSONArray.fromObject(map)

          结果:[{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}]

       将JSONObject放入JsonArray 

          例如:JSONArray.fromObject(jsonObject)

          结果:[{"姓名":"张三","身高":"166","年龄":"13"}]

    获取:

      jsonObject.getJSONObject(key)

      jsonObject.getInt(key)

      jsonObject.getString(key)

      数组:

        jsonArray.getJSONObject(i)

    3.GSON  谷歌

     导入1个gson的jar包
    * 常见对象
      String new Gson().toJson(Object obj);

    Json数据转换为对象

      new Gson().fromJson(Json_string,class)

    4.FastJSON阿里巴巴

    (推荐使用)

    对象:

      JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。

      JSONObject:fastJson提供的json对象。

      JSONArray:fastJson提供json数组对象。

    说明:

      JSONObject当成一个Map<String,Object>来看,只是JSONObject提供了更为丰富便捷的方法

      JSONArray当做一个List<Object>,可以把JSONArray看成JSONObject对象的一个集合

      由于JSONObject和JSONArray继承了JSON,所以说也可以直接使用两者对JSON格式字符串与JSON对象及javaBean之间做转换

    首先定义三个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}]}";

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

    JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
    //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的

    json字符串-数组类型与JSONArray之间的转换

            JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
            //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的
    
            //遍历方式1
            int size = jsonArray.size();
            for (int i = 0; i < size; i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
            }
    
            //遍历方式2
            for (Object obj : jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
                System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
            }

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

         JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
            //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
            
            String teacherName = jsonObject.getString("teacherName");
            Integer teacherAge = jsonObject.getInteger("teacherAge");
            JSONObject course = jsonObject.getJSONObject("course");
            JSONArray students = jsonObject.getJSONArray("students");

    JSON格式字符串与javaBean之间的转换。

    json字符串与javaBean之间的转换推荐使用 TypeReference<T> 这个类,使用泛型可以更加清晰,当然也有其它的转换方式

     Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
     //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因为JSONObject继承了JSON,所以这样也是可以的
    //json字符串-数组类型与javaBean之间的转换
    ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});

    toJSONString

    1,对于JSON对象与JSON格式字符串的转换可以直接用 toJSONString()这个方法。

    2,javaBean与JSON格式字符串之间的转换要用到:JSON.toJSONString(obj);

    3,javaBean与json对象间的转换使用:JSON.toJSON(obj),然后使用强制类型转换,JSONObject或者JSONArray。

     

  • 相关阅读:
    中国专利 相关
    GPU 编程相关 简要摘录
    RDMA 相关 简要摘录
    GO 使用 动态链接库(共享链接库)进行编译 生成动态链接可执行文件
    GO 使用静态链接库编译 生成可执行文件 使用第三方 .a 文件,无源码构造
    好用的性能检测工具-性能监控工具- Glances
    程序运行时间测试
    程序运行时间测试
    程序运行时间测试
    2019-11-29-WPF-绑定命令在-MVVM-的-CanExecute-和-Execute-在按钮点击都没触发可能的原因...
  • 原文地址:https://www.cnblogs.com/soul-wonder/p/8919301.html
Copyright © 2011-2022 走看看