我有coursera api的课程列表:
{"elements":[
{"id":69,
"shortName":"contraception",
"name":"Contraception: Choices, Culture and Consequences",
"links":{}
},
...
]
}
我想将其转换为看起来像这样的文档(我使用 <--- 箭头作为注释):
{"Courses":[
{
"Type" : "Course",
"Title" : "contraception" <---- short name
"Content" : {"id":69, <---- the original course
"shortName":"contraception",
"name":"Contraception: Choices, Culture and Consequences",
"links":{}
}
},
...
]}
是否可以使用仅来自 play 的 json api 执行此操作?这是我目前的做法(转换为 scala 列表)。
val courses = (response.json \ "elements")
.as[List[JsValue]]
.map { course =>
// This is how we want our document to look
Json.obj(
"Type" -> "Course",
"Provider" -> "Coursera",
"Title" -> (course \ "name"),
"Content" -> course
)
}
// then put this into the final json object with "Courses" ...