java如何将这些数据转化为实体类 {"a":"9PtTNNWB1ooPsyQGEqijsg==","b":"","c":"1702024265046"}
时间: 2023-12-11 13:33:13 浏览: 60
可以使用Jackson库将JSON字符串转换为Java对象。具体实现如下所示:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class MyClass {
public static void main(String[] args) throws IOException {
String jsonStr = "{\"a\":\"9PtTNNWB1ooPsyQGEqijsg==\",\"b\":\"\",\"c\":\"1702024265046\"}";
ObjectMapper objectMapper = new ObjectMapper();
MyEntity myEntity = objectMapper.readValue(jsonStr, MyEntity.class);
System.out.println(myEntity);
}
}
class MyEntity {
private String a;
private String b;
private String c;
// 必须要有默认构造函数
public MyEntity() {}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
@Override
public String toString() {
return "MyEntity{" +
"a='" + a + '\'' +
", b='" + b + '\'' +
", c='" + c + '\'' +
'}';
}
}
```
输出结果为:
```
MyEntity{a='9PtTNNWB1ooPsyQGEqijsg==', b='', c='1702024265046'}
```
阅读全文