1

これに対する答えがわかりません - これを行う簡単な方法はありますか? いくつかのオブジェクト x のリストを取得し、ハッキーなコードなしで単一の 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"
    }
  ]
}
4

2 に答える 2

2

これはあなたが意味するものですか?

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)
  }

  val jsonList = Json.toJson(listToConvert)
  Json.stringify(jsonList)
}
于 2013-02-18T11:23:21.507 に答える
0

EEColor のおかげで、私が欲しかったものを手に入れることができました。私は彼に答えを与えましたが、私が使用した最終的なコードは次のとおりです。

  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)
    }

    val jsonList = Json.toJson(listToConvert.toSeq)
    val result = JsObject("data" -> jsonList :: Nil)
    Json.stringify(result)
}
于 2013-02-18T18:15:43.430 に答える