zoukankan      html  css  js  c++  java
  • Json字符串转Json对象及Java对象

    Json字符串转Java对象

    //json 字符串 转Java对象
     String confStr = "{"key":"nihk","secret":"qq123456"}";
                JSONObject jsonObject = JSONObject.parseObject(confStr);
              AuthConf  conf = JSONObject.toJavaObject(jsonObject, AuthConf.class);
            System.out.println("conf=" + conf);
    
    
    // json 数组 字符串 转JSONArray
     String confStr = "[{"key":"nihk"},{"key":"nihk2"}]";
     JSONArray jarr = JSONArray.parseArray(confStr);

    // 以下方法得到的是JSONArray List
    <AuthConf> conf2 = JSONObject.toJavaObject(jarr, List.class); System.out.println("conf2=" + conf2); List<AuthConf> conf22 = JSONArray.toJavaObject(jarr, List.class); System.out.println("conf22=" + conf22); List<AuthConf> conf222 = JSON.toJavaObject(jarr, List.class); System.out.println("conf222=" + conf222); //输出 conf=AuthConf(key=nihk, secret=qq123456) conf2=[{"key":"nihk"},{"key":"nihk2"}] conf22=[{"key":"nihk"},{"key":"nihk2"}] conf222=[{"key":"nihk"},{"key":"nihk2"}]

    说明:

    如果是json 字符串,则采用JSONObject.parseObject(confStr)转换成对象;
    如果是json数组字符串采用JSONArray jarr = JSONArray.parseArray(confStr);

    转换成对象AuthConf conf = JSONObject.toJavaObject(jsonObject, AuthConf.class)
    如果是JSONArray,遍历即可。
  • 相关阅读:
    内存的分页管理方式解释
    分组统计SQL
    NUMA导致的Oracle性能问题
    SQL Server死锁的解决过程
    PHP创建对象的6种方式
    PHP编程20大效率要点
    php设置随机ip访问
    php使用QueryList轻松采集JavaScript动态渲染页面
    正确理解 PHP 的重载
    PHP 核心特性之匿名函数
  • 原文地址:https://www.cnblogs.com/fanbi/p/13897076.html
Copyright © 2011-2022 走看看