龙乾 2020-04-19 11:30 采纳率: 100%
浏览 2736
已采纳

如何将JAVA数组转换成 JSON对象或JSON字符串

自定义对象

public class CourseQuery implements Serializable {

    private static final long serialVersionUID = -4699713095477151086L;

    private String courseId;// 课程编号
    private String courseName;// 课程名称
    private String courseTeacher;// 课程任课老师姓名
    private String courseDate;// 课程上课时间
    private String courseIntroduction;// 课程介绍

    public CourseQuery(String courseId, String courseName, String courseTeacher, String courseDate,
            String courseIntroduction) {
        super();
        this.courseId = courseId;
        this.courseName = courseName;
        this.courseTeacher = courseTeacher;
        this.courseDate = courseDate;
        this.courseIntroduction = courseIntroduction;
    }

    public String getCourseId() {
        return courseId;
    }

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCourseTeacher() {
        return courseTeacher;
    }

    public void setCourseTeacher(String courseTeacher) {
        this.courseTeacher = courseTeacher;
    }

    public String getCourseDate() {
        return courseDate;
    }

    public void setCourseDate(String courseDate) {
        this.courseDate = courseDate;
    }

    public String getCourseIntroduction() {
        return courseIntroduction;
    }

    public void setCourseIntroduction(String courseIntroduction) {
        this.courseIntroduction = courseIntroduction;
    }

    @Override
    public String toString() {
        return "{courseId:" + courseId + ", courseName:" + courseName + ", courseTeacher:" + courseTeacher
                + ", courseDate:" + courseDate + ", courseIntroduction:" + courseIntroduction + "}";
    }

}

比如将一个上述对象的数组装换成一个JSON对象或JSON字符串

形如:

{
"data":
[
{...},{....},{...}
}
}
  • 写回答

2条回答 默认 最新

  • 释怀然已 2020-04-19 22:35
    关注

    你是在用Spring写吗? 是的话mavn添加依赖包,然后@ResponseBody注解返回消息会自动转成json的,

    com.fasterxml.jackson.core
    jackson-databind
    2.8.10

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 快手联盟怎么快速的跑出建立模型
  • ¥20 ros环境下运行orb-slam一直报错
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题
  • ¥60 如何把照片修复成原始拍摄图
  • ¥80 Exited too quickly (process log may have details)
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像