服务器之家

服务器之家 > 正文

java 实现反射 json动态转实体类--fastjson

时间:2021-08-10 10:49     来源/作者:HL_123123

我就废话不多说了,大家还是直接看代码吧~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package test.refect;
public class Student {
    // 姓名
    private String name;
    // 年龄
    private String age;
    // 住址
    private String address;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", address=" + address + "]";
    }
    
    public void sayHello(Book book){
        System.out.println(book.getName());    
    }
}
?
1
2
3
4
5
6
7
8
9
10
package test.refect;
public class Book {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package test.refect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.alibaba.fastjson.JSONObject;
public class ExecuteWithFastJson {
    public static <T> void main(String[] args) {
        
        // Student str --> Student 主类
        String str = "test.refect.Student";
        Class<?> clazz = null;
        try {
            clazz = Class.forName(str);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        // Book实体 str --> Book 参数类
        String bookStr = "test.refect.Book";
        Class<?> bookClazz = null;
        try {
            bookClazz = Class.forName(bookStr);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        // json --> Book 将参数类转为JSONOBJECT
        String bookJson = "{\"name\":\"Java\"}";
        
 
        // 实例化参数类
        T t = (T) JSONObject.parseObject(bookJson, bookClazz);
        
        // 将参数类注入到主类
        Method method =null;
        try {
            method = clazz.getDeclaredMethod("sayHello", bookClazz);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        // 执行主类
        try {
            method.invoke(clazz.newInstance(), t);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

补充:使用fastjson 进行jsonObject转实体类对象

?
1
2
3
4
5
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.7</version>
</dependency>

首先引入相关jar包,

假设有一个实体类User

?
1
2
3
4
public class User{
 private int id;
 private String name; 
}

然后通过fastJson进行类型转换

?
1
2
3
4
5
6
public static void main(String[] args){
  String userString = "{"id":1,"name","lz"}";
  JSONObject userJson = JSONObject.parseObject(userString);
  User user = JSON.toJavaObject(userJson,User.class);
  
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/huanglei1234567890/article/details/80542596

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部