json串 轉 list 方法 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


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM