Scala Play JSON 无法将 List[CaseClass] 转换为 Json String

Scala Play JSON trouble converting List[CaseClass] to Json String

不确定我哪里出错了它正在返回错误:

No Json serializer as JsObject found for type List[QM_Category].

Try to implement an implicit OWrites or OFormat for this type.

[error] Json.stringify(Json.toJsObject(a.categories))

有没有办法为 List[QM_Category] 定义格式?我认为 QM_Categoryformat 会处理 class 的情况,而 play 应该处理 Lists... 我真正想做的就是获取我的列表并将其转换为 json 字符串。非常直接,但我不确定为什么 Play Json 不喜欢我的格式。

这是我的代码:

case class QM_Answer (
    answerid: String,
    answerstring: String,
    answerscore: Int
);

case class QM_Question (
    questionid: String,
    questionscore: Int,
    questiongoal: Int,
    questionstring: String,
    questiontype: String,
    questioncomments: String,
    questionisna: Boolean,
    questionishidden: Boolean,
    failcategory: Boolean,
    failform: Boolean,
    answers: List[QM_Answer]
);

case class QM_Category (
    categoryid: String,
    categoryname: String,
    categoryscore: Int,
    categorygoal: Int,
    categorycomments: String,
    categoryishidden: Boolean,
    failcategory: Boolean,
    questions: List[QM_Question]
);

case class SurveySourceRaw (
    ownerid: String,
    formid: String,
    formname: String,
    sessionid: String,
    evaluator: String,
    userid: String,
    timelinekey: Long,
    surveyid: String,
    submitteddate: Long,
    month: String,
    channel: String,
    categories: List[QM_Category]
);

case class SurveySource (
    ownerid: String,
    formid: String,
    formname: String,
    sessionid: String,
    evaluator: String,
    userid: String,
    timelinekey: Long,
    surveyid: String,
    submitteddate: Long,
    month: String,
    channel: String,
    categories: String
);

implicit val qmAnswerFormat = Json.format[QM_Answer];
implicit val qmQuestionFormat = Json.format[QM_Question];
implicit val qmCategoryFormat = Json.format[QM_Category];
implicit val surveySourceRawFormat = Json.format[SurveySourceRaw];

var surveySourceRaw = sc
    .cassandraTable[SurveySourceRaw]("mykeyspace", "mytablename") 
    .select("ownerid",
            "formid",
            "formname",
            "sessionid",
            "evaluator",
            "userid",
            "timelinekey",
            "surveyid",
            "submitteddate",
            "month",
            "channel",
            "categories")

var surveyRelational = surveySourceRaw
        .map(a => SurveySource
            (
                a.ownerid,
                a.formid,
                a.formname,
                a.sessionid,
                a.evaluator,
                a.userid,
                a.timelinekey,
                a.surveyid,
                a.submitteddate,
                a.month,
                a.channel,
                Json.stringify(Json.toJsObject(a.categories))
            )) 


List[A] 的播放 JSON 格式,给定 A 的格式,encodes/decodes JSON 数组,例如List[String] [ "foo", "bar", "baz" ]。 JSON 数组不是 JSON 对象。

因此,如果您希望 List[QM_Category] 成为字符串化的 JSON(但不一定是 JSON 对象,例如它可以是字符串、数组等),您可以使用 toJson:

Json.stringify(Json.toJson(a.categories))

或者,如果您希望它成为一个 JSON 对象,您需要为 List[QM_Category]OFormatFormat,它要求 JSON 是一个具有字符串属性和 JSON 值的对象(对于 OReads /OWrites).

我几乎不好意思回答这个问题,但有时我会把它弄得太复杂。 答案是将 cassandra 中的列作为字符串而不是 List[QM_Category] 读取。 cassandra 中的列定义为:

categories list<FROZEN<qm.category>>,

我错误地认为我需要从 Cassandra 读取它作为自定义对象列表。然后我需要使用 play json 将 class 格式化为 JSON 然后将其字符串化。

case class SurveySourceRaw (
    ownerid: String,
    formid: String,
    formname: String,
    sessionid: String,
    evaluator: String,
    userid: String,
    timelinekey: Long,
    surveyid: String,
    submitteddate: Long,
    month: String,
    channel: String,
    categories: List[QM_Category]
);

在现实中,我需要做的就是从 cassandra 中读取它作为 String 类型,并且它以字符串化形式出现 json。玩得好 spark cassandra 连接器,玩得好。

case class SurveySourceRaw (
    ownerid: String,
    formid: String,
    formname: String,
    sessionid: String,
    evaluator: String,
    userid: String,
    timelinekey: Long,
    surveyid: String,
    submitteddate: Long,
    month: String,
    channel: String,
    categories: String
);