string转json
string params = "{"projectExtend":"true","formKey":"F000004","edit":"true","detailShow":"false"}";
JSONObject jsonObject = new JSONObject(params);
String detailShow = jsonObject.getStr("detailShow");
list<实体> 转 list < map>
List<Entity> entityList = new ArrayList<>();
List<Map<String, Object>> mapList = entityList.stream()
.map(entity -> {
Map<String, Object> map = new HashMap<>();
map.put("field1", entity.getField1());
map.put("field2", entity.getField2());
return map;
})
.collect(Collectors.toList());
map转实体
String jsonString = JSONObject.toJSONString(map);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
SsRwb ssRwb = jsonObject.toJavaObject(SsRwb.class);
list实体转JSON数组
List<FromFileUploadResponse> annexDatas;
Gson gson = new Gson();
String annexJson = gson.toJson(annexDatas);
list转json
String jsonStr = JSONUtil.toJsonStr(formRuleList);
map转list
public List<JSONObject> listObjConvert(Map<String, String> dataMap){
List<JSONObject> jsonList = new ArrayList<>();
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", key);
jsonObject.put("value", value);
jsonList.add(jsonObject);
}
return jsonList;
}
string转JSON
String jsonStr = JSONUtil.toJsonStr(bpmFormReportList);
string转date
new SimpleDateFormat("yyyy-MM-dd").parse(taskDetectDeadline)
string类型转localdate类型
LocalDate date = LocalDate.parse(cell7.getStringCellValue(),DateTimeFormatter.ofPattern("yyyy-MM-dd"));
string转LocalDateTime类型
final LocalDateTime localDateTime = LocalDate.parse(cell7.getStringCellValue(), DateTimeFormatter.ISO_DATE).atStartOfDay();
date转string
new SimpleDateFormat("yyyy-MM-dd").format(taskDetectDeadline)
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String startTime = fmt.format(entity.getStartTime());
date转LocalDateTime
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
LocalDateTime转LocalDate
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
object转map
public Map<String,Object> obj2Map(Object obj) throws Exception{
Map<String,Object> map=new HashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
}
return map;
}
java把string转成JSON数组
[{"fileId":"1686222279572836354","fileName":"梁伟浩测试上传附件.jpg","extension":"jpg","filePath":"bpm/from-file/INST00001837/1135901806031900672.jpg","fileSize":"18412"},{"fileId":"1686222298426232833","fileName":"小悟空.png","extension":"png","filePath":"bpm/from-file/INST00001837/1135901824725913600.png","fileSize":"22254"}]
JSONArray jsonArray = new JSONArray(value);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String checkBoxValue = jsonObject.getStr("value");
String ifCheckBox = jsonObject.getStr("checked");
boolean aBoolean = Boolean.parseBoolean(ifCheckBox);
checkColorMap.put(checkBoxValue, aBoolean);
}
list
HashMap<String, Object> map = (HashMap) oldFormDatum;