高端网站建设1,石排镇网站建设公司,做网站推广怎么说广告词,知名企业有哪些文章目录 准备工作1.JSONObject获取所有的key2.集合中实体对象转换 list中Enrey转Dto3.字符串转ListBusyTimeIndicatorAlarmThreshold4.json字符串转JSONObject5.list根据ids数组过滤list6.json字符串转JavaBean对象7.json对象转javabean8.jsonObject转map9.List\U… 文章目录 准备工作1.JSONObject获取所有的key2.集合中实体对象转换 list中Enrey转Dto3.字符串转ListBusyTimeIndicatorAlarmThreshold4.json字符串转JSONObject5.list根据ids数组过滤list6.json字符串转JavaBean对象7.json对象转javabean8.jsonObject转map9.List\User转jsonArray10.jsonArray转成String[]问题为啥使用int就判断失效而使用Integer和String都能准确判断 本人其他文章链接 准备工作 引入pom dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.78/version
/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.22/version/dependency 实体bean import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;Data
AllArgsConstructor
NoArgsConstructor
public class User {private Integer id;//姓名private String name;
}1.JSONObject获取所有的key
技巧
JSONObject获取key↓JSONObject objfor (Map.EntryString, Object entry : cutReceiveAlarmMessageObject.entrySet()) {String s entry.getKey();}2.集合中实体对象转换 list中Enrey转Dto
list中Enrey转Dto↓ListWarningNoticeDto warningNoticeDtoList warningNoticeList.getInfo().getList().stream().map(this::getEntryToDto).collect(Collectors.toList());/*** entry转DTO* param warningNotice entry* return dto*/private WarningNoticeDto getEntryToDto(WarningNotice warningNotice) {WarningNoticeDto warningNoticeDto new WarningNoticeDto();BeanUtils.copyProperties(warningNotice, warningNoticeDto);return warningNoticeDto;}3.字符串转List
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.JSONObject;String str [{id: 5,nodeIdArr: [\221\,\222\],nodeNameArr: [\enb_221\,\2222\],upperLimitOfTheBusyTimeThreshold: 9,lowerLimitOfTheBusyTimeThreshold: 2,dateRangeBeginTime: 1701648000000,dateRangeEndTime: 1701682200000,createTime: 1701676594000,updateTime: 1701737385000,activeState: 1},{id: 6,nodeIdArr: [\2003\,\501\,\10010\],nodeNameArr: [\CityA\,\501\,\Vir1\],upperLimitOfTheBusyTimeThreshold: 9,lowerLimitOfTheBusyTimeThreshold: 2,dateRangeBeginTime: 1701648000000,dateRangeEndTime: 1701682200000,createTime: 1701676641000,updateTime: 1701737382000,activeState: 1}]
ListBusyTimeIndicatorAlarmThreshold busyTimeIndicatorAlarmThresholdList new ArrayList();
busyTimeIndicatorAlarmThresholdList JSONObject.parseObject(str, new TypeReferenceListBusyTimeIndicatorAlarmThreshold() {});方式一、List busyTimeIndicatorAlarmThresholdList new ArrayList(); busyTimeIndicatorAlarmThresholdList JSONObject.parseObject(str, new TypeReferenceList() {}); 方式二、List userList JSONArray.parseArray(str, User.class); 4.json字符串转JSONObject
Test
public void jsonStrConverJSONObject(){String str {\id\:1,\name\:\tom\};JSONObject jsonObject JSONObject.parseObject(str);System.out.println(jsonObject);
}输出{“name”:“tom”,“id”:1} 5.list根据ids数组过滤list
Test
public void listFilter() {ListUser list new ArrayList();list.add(new User(1, a));list.add(new User(2, b));list.add(new User(3, c));list.add(new User(4, d));list.add(new User(5, e));list.add(new User(6, f));list.add(new User(7, g));list.add(new User(8, h));list.add(new User(9, i));list.add(new User(10, j));//注意数组类型必须使用Integer才可以使用int会判断失败Integer[] arr new Integer[]{1,2,5,6,9};ListUser filterList list.stream().filter(item - Arrays.asList(arr).contains(item.getId())).collect(Collectors.toList());filterList.stream().forEach(System.out::println);
}6.json字符串转JavaBean对象
Test
public void jsonStrConverBean(){String str {\id\:1,\name\:\tom\};User user JSONObject.parseObject(str, User.class);System.out.println(user);
}输出User(id1, nametom) 7.json对象转javabean
Test
public void JSONObjectConverBean(){JSONObject jsonObject new JSONObject();jsonObject.put(id, 1);jsonObject.put(name, tom);User user JSONObject.toJavaObject(jsonObject, User.class);System.out.println(user);
}输出User(id1, nametom) 8.jsonObject转map
Test
public void JSONObjectConverMap(){JSONObject jsonObject new JSONObject();jsonObject.put(id, 1);jsonObject.put(name, tom);MapString,String map JSONObject.parseObject(jsonObject.toJSONString(), Map.class);System.out.println(map);
}输出{nametom, id1} 9.ListUser转jsonArray
Test
public void listConverjJsonArray(){ListUser list new ArrayList();list.add(new User(1, a));list.add(new User(2, b));//错误写法因为list.toString()输出[User(id1, namea), User(id2, nameb)]。这东西无法json解析会报错com.alibaba.fastjson.JSONException: syntax error, pos 2, line 1, column 3[User(id1, namea), User(id2, nameb)]// JSONArray jsonArray JSONArray.parseArray(list.toString());//正确写法简写方式JSONArray jsonArray JSONArray.parseArray(JSONObject.toJSONString(list));//正确写法复杂方式// JSONArray jsonArray new JSONArray();// JSONObject jsonObject null;// for (User user: list) {// jsonObject new JSONObject();// jsonObject.put(id, user.getId());// jsonObject.put(name, user.getName());// jsonArray.add(jsonObject);// }System.out.println(jsonArray);
}10.jsonArray转成String[]
Test
public void jsonArrayConverStringArray(){JSONArray jsonArray new JSONArray();jsonArray.add(0, 100);jsonArray.add(1, 101);jsonArray.add(2, 102);System.out.println(jsonArray: jsonArray);String[] stringArr new String[jsonArray.size()];for (int i 0; i jsonArray.size(); i) {stringArr[i] jsonArray.get(i).toString();}for(String str : stringArr) {System.out.println(str);}
}问题为啥使用int就判断失效而使用Integer和String都能准确判断
/*** 问题为啥使用int就判断失效而使用Integer和String都能准确判断* 答案不能将基本数据类型转化为List列表。
*/
Test
public void test1() {int[] arr new int[]{1,2,5,6,9};System.out.println(Arrays.asList(arr).contains(1)); //结果为falseInteger[] arr2 new Integer[]{1,2,5,6,9};System.out.println(Arrays.asList(arr2).contains(1)); //结果为trueString[] arr3 new String[]{1,2,5,6,9};System.out.println(Arrays.asList(arr3).contains(1)); //结果为true//验证答案如下,把arr、arr2、arr3分别返回查看返回泛型能够看出Arrays.asList(arr)返回的居然是Listint[]问题就出在这说明list里面包含的是一个个的int[]用这个判断ints.contains(1)肯定为falseListint[] ints Arrays.asList(arr);ListInteger integers Arrays.asList(arr2);ListString strings Arrays.asList(arr3);
}本人其他文章链接
1.java小工具util系列1日期毫秒数转日期字符串 https://blog.csdn.net/a924382407/article/details/121955349
2.java小工具util系列2获取字符modelStr在字符串str中第count次出现时的下标 https://blog.csdn.net/a924382407/article/details/121955455
3.java小工具util系列3正则表达式匹配:匹配不包含特殊字符的字符串 https://blog.csdn.net/a924382407/article/details/121955737
4.java小工具util系列4String[] 转 List Integer https://blog.csdn.net/a924382407/article/details/121956201
5.java小工具util系列5基础工具代码(Msg、PageResult、Response、常量、枚举) https://blog.csdn.net/a924382407/article/details/120952865
6.java小工具util系列6java执行string返回boolean结果 https://blog.csdn.net/a924382407/article/details/117124536
7.java小工具util系列7集合中实体对象转换 list中Enrey转Dto https://blog.csdn.net/a924382407/article/details/121957545
8.java小工具util系列8JSONObject获取key https://blog.csdn.net/a924382407/article/details/121957607
9.java小工具util系列9检测一个字符串是否是时间格式 https://blog.csdn.net/a924382407/article/details/123948881
10.java小工具util系列10时间毫秒数、时间格式字符串、日期之间相互转化 https://blog.csdn.net/a924382407/article/details/124581851