JSON对象转字符串

系统管理员 2023-06-22 09:28 93阅读 0赞

1、在pom.xml中添加依赖包

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

2、创建测试类(JSONTest8.java)

  1. package com.jeff;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.jeff.entity.User;
  5. public class JSONTest8 {
  6. public static void main(String[] args) {
  7. User user = new User("张三", "123456", 18);
  8. JSONObject jsonObject = (JSONObject) JSON.toJSON(user);
  9. jsonObjectToString(jsonObject);
  10. }
  11. /** * * @description: JSON对象转JSON字符串 * @author: Jeff * @date: 2019年12月15日 * @param jsonObject JSON对象 */
  12. public static void jsonObjectToString(JSONObject jsonObject) {
  13. // JSON对象转换为JSON字符串
  14. String jsonString = jsonObject.toJSONString();
  15. System.out.println(jsonString);
  16. }
  17. }

3、控制台输出结果
在这里插入图片描述

发表评论

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

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

相关阅读