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
本文来自博客园,作者:aspirant,转载请注明原文链接:https://www.cnblogs.com/aspirant/p/12462692.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2019-03-11 阿里p3c(代码规范,eclipse插件、模版,idea插件)
2019-03-11 mac 电脑 打开隐藏文件
2019-03-11 groovy安装 ideal