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,遍历即可。
  • 相关阅读:
    JVM运行内存分配和回收
    关于评审--从思想到落地
    从浏览器或者Webview 中唤醒APP
    控制台的艺术(附原理实现)
    深入研究嵌入式web服务器的视频监控应用
    css中url的路径含义及使用
    epoll
    C++中智能指针的设计和使用
    wget命令的使用
    UPNP
  • 原文地址:https://www.cnblogs.com/fanbi/p/13897076.html
Copyright © 2011-2022 走看看