Object类转成json字符串
时间: 2023-07-26 14:47:04 浏览: 101
要将Object类转成json字符串,可以使用JSON库。以下是一个示例代码,可以将一个Object对象转成json字符串:
```java
import com.alibaba.fastjson.JSON;
public class Demo {
public static void main(String[] args) {
// 创建一个Object对象
Object obj = new Object();
// 将Object对象转成json字符串
String jsonStr = JSON.toJSONString(obj);
// 输出json字符串
System.out.println(jsonStr);
}
}
```
需要注意的是,如果Object对象中包含了非基本类型的成员变量,那么这些成员变量也需要实现序列化接口,否则转换时会报错。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)