zoukankan      html  css  js  c++  java
  • json串 转 list<class> 方法 List转JSONArray和JSONArray转List

     

    1.List转JSONArray


     

    List<T> list = new ArrayList<T>();
    JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));


    2.JSONArray转List

    JSONArray array = new JSONArray();
    List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);


     
    3.String转JSONArray

    String st = "[{name:Tim,age:25,sex:male},{name:Tom,age:28,sex:male},{name:Lily,age:15,sex:female}]";
    JSONArray tableData = JSONArray.parseArray(st);

    这是我写的;

      public static List<ProjectRestDTO> setMap(){
    
    
            String abc="[{"tenantId":15203,"projectId":328001015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":357001015203,"price":[1990,2990]}]},{"tenantId":15203,"
                + ""projectId":355001015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":398001015203,"price":[10000]}]},{"tenantId":15203,"
                + ""projectId":327001015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":356002015203,"price":[10000]}]},{"tenantId":15203,"
                + ""projectId":212001015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":224001015203,"price":[100,300]}]},{"tenantId":15203,"
                + ""projectId":366005015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":414003015203,"price":[1000,2000]}]},{"tenantId":15203,"
                + ""projectId":365006015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":414006015203,"price":[3000,4000]}]},{"tenantId":15203,"
                + ""projectId":365003015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":414004015203,"price":[100,200]}]},{"tenantId":15203,"
                + ""projectId":315001015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":344003015203,"price":[1]},{"eventId":345001015203,"price":[1,"
                + "10000]},{"eventId":345002015203,"price":[10000,15000]}]},{"tenantId":15203,"
                + ""projectId":590001015203,"marketChannel":33521,"channelType":1,"
                + ""eventList":[{"eventId":717001015203,"price":[100,200]},{"eventId":719002015203,"
                + ""price":[100,200]},{"eventId":726002015203,"price":[100,200]}]},"
                + "{"eventList":[{"eventId":287001015203,"price":[100,200,300]}],"marketChannel":33521,"
                + ""tenantId":15203,"channelType":1,"projectId":253003015203}]";
    
           // List<ProjectRestDTO> nn=new ArrayList<>();
           //
           //ProjectRestDTO aa= assemble();
           //nn.add(aa);
           //EventPriceDTO eventPriceDTO=new EventPriceDTO();
           //eventPriceDTO.setEventId(287001015203L);
           //eventPriceDTO.setPrice(Arrays.asList(100L,200L,300L));
           //
           //ProjectRestDTO projectRestDTO=aa;
           //projectRestDTO.setProjectId(253003015203L);
           //projectRestDTO.setEventList(Arrays.asList(eventPriceDTO));
           //nn.add(projectRestDTO);
    
            //JSONArray array = new JSONArray();
            List<ProjectRestDTO> list = JSONObject.parseArray(abc, ProjectRestDTO.class);
    
            //List<ProjectRestDTO> jsonObject =new JSONArray(abc);
    
            //List<ProjectRestDTO> list = JSONArray.toArray(abc,ProjectRestDTO.class);
            //String aa1 = jsonObject.toString();
    
            //System.out.println(aa1);
    
    
            return list;
    
        }

    参考:List转JSONArray和JSONArray转List


     

  • 相关阅读:
    LED自动同步时钟(LED电子时钟系统)京准电子
    电力北斗对时装置(北斗时钟同步系统)详细介绍
    京准时间同步系统(NTP网络同步时钟)概述
    京准:NTP时钟服务器(NTP时钟服务器)研究分析
    医院时钟系统(子母钟系统)设计组成架构
    GPS校时器(北斗授时设备)助力政务云建设
    三大免费开源的php语言cms系统 用好它们让你一天建好一个网站
    想精通正则表达式 这几个正则表达式学习资料及工具你必须有!
    零基础建站如何配置PHP运行环境 几种服务器环境配置的选择和方法
    php代码如何加域名授权?开源php项目如何保护版权 商业授权?
  • 原文地址:https://www.cnblogs.com/aspirant/p/12462692.html
Copyright © 2011-2022 走看看