我看不到答案 - 有沒有簡單的方法來做到這一點?我想獲取一些對象x的列表,並將其轉換爲單個json對象,而不需要任何hacky代碼?我正在做一些醜陋的東西,但希望將這個列表放入一個名爲'data'的對象中。我可以以某種方式將其映射到對象「數據」?Play2和Scala(Jerkson) - JsObject的列表 - 如何轉換爲Json對象
private def renderArticleJson(articles: Iterable[GraphedArticle]): String = {
val listToConvert = for (article <- articles) yield {
JsObject(
"articleId" -> Json.toJson(article.getArticleId)
:: "articleUrl" -> Json.toJson(article.getArticleUrl)
:: "graphId" -> Json.toJson(article.asVertex().getId.toString)
:: "fullName" -> Json.toJson(article.getTitle)
:: "imageUrl" -> Json.toJson(article.getImageUrl)
:: Nil
)
}
}
編輯的要求:增加了什麼,我想出去(現在解決由於第一個答案的幫助)
{
"data": [
{
"articleId": null,
"articleUrl": null,
"graphId": "#8:24",
"fullName": "hey",
"imageUrl": "hey"
},
{
"articleId": null,
"articleUrl": null,
"graphId": "#8:25",
"fullName": "hey",
"imageUrl": "hey"
},
{
"articleId": "b23c162d-b0af-4ce3-aebf-f33943492f95",
"articleUrl": null,
"graphId": "#8:26",
"fullName": "hey",
"imageUrl": "hey"
},
{
"articleId": "8afe310c-8337-4a8a-8406-5670249ba0a7",
"articleUrl": "hey",
"graphId": "#8:27",
"fullName": "hey",
"imageUrl": "hey"
}
]
}
你可以添加你想看到的結果字符串嗎? – EECOLOR 2013-02-17 23:51:47
編輯 - 謝謝 – JasonG 2013-02-18 18:13:34