Java Bean、Json、Map和List互转方式汇总

这里只汇总用现有工具类实现的方式,方便以后查看,当然可能不是很完整,后续有知道的会添加进来

Bean转Bean

  • Spring
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);
  • hutool工具包
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转Map

  • org.apache.commons.beanutils
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Map<String, String> map = BeanUtils.describe(bean);
System.out.println(map);
  • hutool工具包
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
Map<String, Object> map = BeanUtil.beanToMap(bean);
System.out.println(map);
  • org.apache.commons.beanutils
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
BeanMap beanMap = new BeanMap(bean);
System.out.println(beanMap);
  • Jackson
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);
  • json-lib
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);

Map转Bean

  • org.apache.commons.beanutils
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);
  • hutool
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);
  • Jackson
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);
  • json-lib
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转Json字符串

  • fastJson
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = JSONObject.toJSONString(bean);
System.out.println(json);
  • hutool
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = JSONUtil.toJsonStr(bean);
System.out.println(json);
  • Jackson
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = new ObjectMapper().writeValueAsString(bean);
System.out.println(json);
  • gson
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);
  • json-lib
Bean bean = new Bean();
bean.setId("123");
bean.setName("huang");
bean.setAge(25);
String json = JSONObject.fromObject(bean).toString();
System.out.println(json);

Json字符串转Bean

  • Jackson
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = new ObjectMapper().readValue(json, Bean.class);
System.out.println(bean);
  • JsonPath
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = JsonPath.parse(json).read("$", Bean.class);
System.out.println(bean);
  • fastJson
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = JSON.parseObject(json, Bean.class);
System.out.println(bean);
  • gson
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Gson gson = new GsonBuilder().create();
Bean bean = gson.fromJson(json, Bean.class);
System.out.println(bean);
  • hutool
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = JSONUtil.toBean(json, Bean.class);
System.out.println(bean);
  • json-lib
String json = "{\"name\":\"huang\",\"id\":\"1234\",\"age\":25}";
Bean bean = (Bean) JSONObject.toBean(JSONObject.fromObject(json), Bean.class);
System.out.println(bean);

Map转Json字符串

  • fastJson
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);
  • hutool
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);
  • Jackson
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);
  • gson
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);
  • json-lib
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);
  • net.minidev.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字符串转Map

与上面的Json字符串转Bean方法一样,只不过将Bean.class换成Map.class

json转list对象

@Data
public class AdapterParam {

    private String name;

    private String description;
}
  • fastJson
String json = "[{\"name\":\"p1\",\"description\":\"参数1\"},{\"name\":\"p2\",\"description\":\"参数2\"}]";
List<AdapterParam> list = JSONObject.parseArray(json, AdapterParam.class);
System.out.println(list);
  • hutool
String json = "[{\"name\":\"p1\",\"description\":\"参数1\"},{\"name\":\"p2\",\"description\":\"参数2\"}]";
List<AdapterParam> list = JSONUtil.toList(JSONUtil.parseArray(json), AdapterParam.class);
System.out.println(list);
  • Jackson
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);
  • gson
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);

List对象转Json字符串

跟上面的Bean转Json以及Map转Json方法一样。

你可能感兴趣的:(Java)