JAVA 对象转换为JSON

蔚落 2024-03-23 23:06 104阅读 0赞

转载:如何把java对象转换为json java对象怎么转成json_clghxq的技术博客_51CTO博客

1、Java对象列表转换为JSON对象数组,并转为字符串

JSONArray jsonArray = JSONArray.fromObject(list);
String jsonArrayStr = jsonArray.toString();

2、把Java对象转换成JSON对象,并转化为字符串

JSONObject jsonObject = JSONObject.fromObject(obj);
String jsonObjectStr = jsonObject.toString();

3、过滤不需要转换为JSON格式的属性

使用jsonConfig对象的setExcludes()方法,传入参数为待过滤属性组成的数组。

JsonConfig cfg = new JsonConfig();
cfg.setExcludes(new String[] {“待过滤属性1”, “待过滤属性2”, …, “待过滤属性n”});

  1. json转bean

    JSONObject.toBean(targetJson, targetClass);

5、实例

package com.ajax.test;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class Customer {
public Customer(String name, String id) {
super();
this.name = name;
this.id = id;
}
private String name;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public static void main(String[] args) throws JsonProcessingException {
//包含多个对象的List集合转换为JSON格式
List list = new ArrayList();
Customer c1 = new Customer(“Alice”, “001”);
Customer c2 = new Customer(“Bruce”, “002”);
Customer c3 = new Customer(“Cindy”, “003”);
list.add(c1);
list.add(c2);
list.add(c3);
JsonConfig config1 = new JsonConfig();
//过滤List集合中的Customer对象的id属性不生成JSON
config1.setExcludes(new String[] {“id”});
JSONArray jsonArray = JSONArray.fromObject(list, config1);
System.out.println(jsonArray.toString());
//一个对象转换为JSON格式
Customer c = new Customer(“Boss”, “004”);
JsonConfig config2 = new JsonConfig();
//过滤Customer对象的id属性不生成JSON
config2.setExcludes(new String[] {“id”});
JSONObject jsonObject = JSONObject.fromObject(c, config2);
System.out.println(jsonObject.toString());
}
}

6.将父类对象转化为子类对象:

创建父类实例,将父类实例化

将子类实例转化成json

将父类实例转化成json

遍历父类json实例,使用子类json获取vaule值,设置到父类json中。

代码如下:

  1. public static Object createBeanWith(Class targetClass, Object source) {
  2. Object target = null;
  3. try {
  4. target = targetClass.newInstance();
  5. } catch (Exception e) {
  6. return null;
  7. }
  8. JSONObject targetJson = JSONObject.fromObject(target);
  9. JSONObject sourceJson = JSONObject.fromObject(source);
  10. for (Object key : targetJson.keySet()) {
  11. if (sourceJson.containsKey(key)) {
  12. targetJson.put(key, sourceJson.get(key));
  13. }
  14. }
  15. return JSONObject.toBean(targetJson, targetClass);
  16. }

精简写法:

  1. Object result = JSONObject.parseObject(JSONObject.toJSONString(source), targetClass);

maven依赖的包:

  1. <!--JSON-->
  2. <dependency>
  3. <groupId>net.sf.json-lib</groupId>
  4. <artifactId>json-lib</artifactId>
  5. <version>2.4</version>
  6. <classifier>jdk15</classifier>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.json</groupId>
  10. <artifactId>json</artifactId>
  11. <version>20230227</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
  14. <dependency>
  15. <groupId>net.sf.ezmorph</groupId>
  16. <artifactId>ezmorph</artifactId>
  17. <version>1.0.6</version>
  18. </dependency>
  19. <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
  20. <dependency>
  21. <groupId>commons-beanutils</groupId>
  22. <artifactId>commons-beanutils</artifactId>
  23. <version>1.9.4</version>
  24. </dependency>
  25. <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
  26. <dependency>
  27. <groupId>commons-collections</groupId>
  28. <artifactId>commons-collections</artifactId>
  29. <version>3.2.2</version>
  30. </dependency>
  31. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
  32. <dependency>
  33. <groupId>org.apache.commons</groupId>
  34. <artifactId>commons-lang3</artifactId>
  35. <version>3.4</version>
  36. </dependency>
  37. <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
  38. <dependency>
  39. <groupId>commons-lang</groupId>
  40. <artifactId>commons-lang</artifactId>
  41. <version>2.6</version>
  42. </dependency>
  43. <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  44. <dependency>
  45. <groupId>commons-logging</groupId>
  46. <artifactId>commons-logging</artifactId>
  47. <version>1.2</version>
  48. </dependency>
  49. <!--用于解析json-->
  50. <dependency>
  51. <groupId>commons-io</groupId>
  52. <artifactId>commons-io</artifactId>
  53. <version>2.4</version>
  54. </dependency>

7、使用hutool工具类

  1. import cn.hutool.json.JSONObject;
  2. import cn.hutool.json.JSONUtil;
  3. public static Object createBeanWith(Class targetClass, Object source) {
  4. Object target = null;
  5. try {
  6. target = targetClass.newInstance();
  7. } catch (Exception e) {
  8. return null;
  9. }
  10. JSONObject targetJson = JSONUtil.parseObj(target, false);
  11. JSONObject sourceJson = JSONUtil.parseObj(source, false);
  12. for (String key : targetJson.keySet()) {
  13. if (sourceJson.containsKey(key)) {
  14. targetJson.set(key, sourceJson.get(key));
  15. }
  16. }
  17. return JSONUtil.toBean(targetJson, targetClass);
  18. }

8、 使用Spring类的方法:

  1. /**
  2. * 这种方式是用了Spring的工具类, 不关乎是否有继承关系,
  3. * 只要有相同的属性就会拷贝进去
  4. */
  5. Foo foo = new Foo();
  6. Son son = new Son();
  7. BeanUtils.copyProperties(foo, son);

参考:

父类转换成子类, 或者是类之间属性拷贝_父类对象转换为子类对象_孔先生在吗的博客-CSDN博客

发表评论

表情:
评论列表 (有 0 条评论,104人围观)

还没有评论,来说两句吧...

相关阅读