这里只汇总用现有工具类实现的方式,方便以后查看,当然可能不是很完整,后续有知道的会添加进来
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Bean2 bean2 = new Bean2();
BeanUtils.copyProperties(bean, bean2);
System.out.println(bean2);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Bean2 bean2 = new Bean2();
BeanUtil.copyProperties(bean, bean2);
System.out.println(bean2);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Map<String, String> map = BeanUtils.describe(bean);
System.out.println(map);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Map<String, Object> map = BeanUtil.beanToMap(bean);
System.out.println(map);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
BeanMap beanMap = new BeanMap(bean);
System.out.println(beanMap);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Map map = new ObjectMapper().convertValue(bean, Map.class);
System.out.println(map);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Map map = (Map) JSONObject.toBean(JSONObject.fromObject(bean), Map.class);
System.out.println(map);
Bean bean = new Bean();
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
BeanUtils.populate(bean, map);
System.out.println(bean);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
Bean bean = BeanUtil.toBean(map, Bean.class);
System.out.println(bean);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
Bean bean = new ObjectMapper().convertValue(map, Bean.class);
System.out.println(bean);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
Bean bean = (Bean) JSONObject.toBean(JSONObject.fromObject(map), Bean.class);
System.out.println(bean);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = JSONObject.toJSONString(bean);
System.out.println(json);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = JSONUtil.toJsonStr(bean);
System.out.println(json);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = new ObjectMapper().writeValueAsString(bean);
System.out.println(json);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(bean);
System.out.println(json);
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = JSONObject.fromObject(bean).toString();
System.out.println(json);
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = new ObjectMapper().readValue(json, Bean.class);
System.out.println(bean);
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = JsonPath.parse(json).read("$", Bean.class);
System.out.println(bean);
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = JSON.parseObject(json, Bean.class);
System.out.println(bean);
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Gson gson = new GsonBuilder().create();
Bean bean = gson.fromJson(json, Bean.class);
System.out.println(bean);
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = JSONUtil.toBean(json, Bean.class);
System.out.println(bean);
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = (Bean) JSONObject.toBean(JSONObject.fromObject(json), Bean.class);
System.out.println(bean);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
String json = JSONObject.toJSONString(map);
System.out.println(json);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
String json = JSONUtil.toJsonStr(map);
System.out.println(json);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
String json = new ObjectMapper().writeValueAsString(map);
System.out.println(json);
Map<String, Object> map = new HashMap<>(3);
map.put("id","123");
map.put("name", "huang");
map.put("age", 25);
Gson gson = new GsonBuilder().create();
String json = gson.toJson(map);
System.out.println(json);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
String json = JSONObject.fromObject(map).toString();
System.out.println(json);
Map<String, Object> map = new HashMap<>(3);
map.put("id", "123");
map.put("name", "huang");
map.put("age", 25);
String json = JSONObject.toJSONString(map);
System.out.println(json);
与上面的Json字符串转Bean方法一样,只不过将Bean.class换成Map.class
@Data
public class AdapterParam {
private String name;
private String description;
}
String json = "[{\"name\":\"p1\",\"description\":\"参数1\"},{\"name\":\"p2\",\"description\":\"参数2\"}]";
List<AdapterParam> list = JSONObject.parseArray(json, AdapterParam.class);
System.out.println(list);
String json = "[{\"name\":\"p1\",\"description\":\"参数1\"},{\"name\":\"p2\",\"description\":\"参数2\"}]";
List<AdapterParam> list = JSONUtil.toList(JSONUtil.parseArray(json), AdapterParam.class);
System.out.println(list);
String json = "[{\"name\":\"p1\",\"description\":\"参数1\"},{\"name\":\"p2\",\"description\":\"参数2\"}]";
TypeReference<List<AdapterParam>> typeReference = new TypeReference<List<AdapterParam>>() {};
List<AdapterParam> list = new ObjectMapper().readValue(json, typeReference);
System.out.println(list);
String json = "[{\"name\":\"p1\",\"description\":\"参数1\"},{\"name\":\"p2\",\"description\":\"参数2\"}]";
Gson gson = new Gson();
List<AdapterParam> list = gson.fromJson(json, new TypeToken<List<AdapterParam>>() {}.getType());
System.out.println(list);
跟上面的Bean转Json以及Map转Json方法一样。