json串 转 list<class> 方法 List转JSONArray和JSONArray转List String 转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;

    }

 

 

String  转 List

  //if (StringUtils.isNotBlank(addChannelDO.getSharedMerchantWhiteList())) {
        //    List<Long> merchantList = Arrays.asList(addChannelDO.getSharedMerchantWhiteList()
        //        .split(",")).stream().map(x -> StringUtils.isNumeric(x) ? Long.valueOf(x) : null)
        //        .collect(Collectors.toList());
        //    saveOrUpdateParam.setMerchantList(merchantList);
        //}

 

 

参考:List转JSONArray和JSONArray转List


 

posted @ 2020-03-11 15:31  aspirant  阅读(3899)  评论(0编辑  收藏  举报