package com.royan.weakey.platform.common.jdk18; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.royan.weakey.platform.dataobject.ContactDO; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; /** * json 工具的使用 * 1)json字符串转为具体的对象 * 2) 对象转为json串 * 3) 集合转为json数组 * 4) json数组转为集合 */ @Slf4j public class JsonUtil { private String name = "{\"name\":\"bai\",\"wo\":\"中国人\"}\n"; public static <T> T jsonStrToObject(String str, Class<T> clz) { log.info("str--{}", str); T t = JSON.parseObject(str, clz); return t; } public static <T> List<T> jsonStrToList(String str, Class<T> clz) { log.info("str--{}", str); List<T> list = JSON.parseArray(str, clz); return list; } public static <T> String objToJsonStr(T t) { log.info("t--{}", JSON.toJSONString(t, SerializerFeature.DisableCircularReferenceDetect)); return JSON.toJSONString(t, SerializerFeature.DisableCircularReferenceDetect); } public static <T> String listTOJsonStr(List<T> t) { log.info("t--{}", JSON.toJSONString(t, SerializerFeature.DisableCircularReferenceDetect)); return JSON.toJSONString(t, SerializerFeature.DisableCircularReferenceDetect); } public static void main(String[] args) { ContactDO contactDO = new ContactDO(); contactDO.setOwnerId("ownerId"); contactDO.setContactPhone("contactPhone"); contactDO.setContactId("contactId"); contactDO.setContactName("contactName"); objToJsonStr(contactDO); log.info("\n"); List<ContactDO> list = new ArrayList<>(); list.add(contactDO); list.add(contactDO); list.add(contactDO); listTOJsonStr(list); log.info("\n"); String str = "{\"contactId\":\"contactId\",\"contactName\":\"contactName\",\"contactPhone\":\"contactPhone\",\"ownerId\":\"ownerId\"}"; ContactDO contactDO1 = jsonStrToObject(str, ContactDO.class); log.info("contactDO1,contactName:{},setOwnerId:{}", contactDO1.getContactName(), contactDO1.getOwnerId()); log.info("\n"); String arrayStr = "[{\"contactId\":\"contactId\",\"contactName\":\"contactName\"," + "\"contactPhone\":\"contactPhone\",\"ownerId\":\"ownerId\"},{\"contactId\":\"contactId\",\"contactName\":\"contactName\",\"contactPhone\":\"contactPhone\",\"ownerId\":\"ownerId\"},{\"contactId\":\"contactId\",\"contactName\":\"contactName\",\"contactPhone\":\"contactPhone\",\"ownerId\":\"ownerId\"}]\n"; List<ContactDO> contactDOS = jsonStrToList(arrayStr, ContactDO.class); log.info("list.get())-{}", contactDOS.get(0).toString()); } }
package com.royan.weakey.platform.dataobject; import com.royan.weakey.platform.common.dto.AbstractBaseDO; import lombok.Data; /** * 联系人关系实体类 */ @Data public class ContactDO extends AbstractBaseDO { /** * 联系人姓名 */ private String contactName; /** * 联系人电话 */ private String contactPhone; /** * owner电话 */ private String contactId; /** * owner */ private String ownerId; @Override public String toString() { return "ContactDO{" + "contactName='" + contactName + '\'' + ", contactPhone='" + contactPhone + '\'' + ", contactId='" + contactId + '\'' + ", ownerId='" + ownerId + '\'' + '}'; } }