【问题标题】:creating json from elixir ecto associations从 elixir ecto 关联创建 json
【发布时间】:2014-11-06 16:07:36
【问题描述】:

我想从 phoenix 中的 ecto 关联生成 JSON。

这是我的协会:

defmodule Blog.Post do
  use Ecto.Model

  schema "posts" do
    field :title, :string
    field :body, :string
    has_many :comments, Blog.Comment
  end
end

和:

defmodule Blog.Comment do
  use Ecto.Model

  schema "comments" do
    field :content, :string
    belongs_to :post, Blog.Post
  end
end

当我生成没有关联的 json 时,结果是这样的:

[%Blog.Post{body: "this is the very first post ever!", id: 1,title: "first post"},
 %Blog.Post{body: "Hello nimrod!!!!", id: 12, title: "hi Nimrod"},
 %Blog.Post{body: "editing the body!!!!", id: 6, title: "hello(edit)"}]

json 看起来像这样

{"posts": [
    {
        "title": "first post",
        "id": 1,
        "body": "this is the very first post ever!"
    },
    {
        "title": "hi Nimrod",
        "id": 12,
        "body": "Hello nimrod!!!!"
    },
    {
        "title": "hello(edit)",
        "id": 6,
        "body": "editing the body!!!!"
    }
]}

但是有了关联,结果是这样的

[%Blog.Post{body: "this is the very first post ever!",
 comments: {Ecto.Associations.HasMany.Proxy,
 #Ecto.Associations.HasMany<[name: :comments, target: Blog.Post,
 associated: Blog.Comment, references: :id, foreign_key: :post_id]>}, id: 1,
 title: "first post"},
 %Blog.Post{body: "Hello nimrod!!!!",
 comments: {Ecto.Associations.HasMany.Proxy,
 #Ecto.Associations.HasMany<[name: :comments, target: Blog.Post,
 associated: Blog.Comment, references: :id, foreign_key: :post_id]>}, id: 12,
 title: "hi Nimrod"},
 %Blog.Post{body: "editing the body!!!!",
 comments: {Ecto.Associations.HasMany.Proxy,
 #Ecto.Associations.HasMany<[name: :comments, target: Blog.Post,
 associated: Blog.Comment, references: :id, foreign_key: :post_id]>}, id: 6,
 title: "hello(edit)"}]

使用上述输出,我无法创建正确的 json 输出。我希望 json 看起来像这样

{"posts": [
     {
        "title": "the title",
        "id": 1,
        "body": "the body",
        "comments": [{"content": "a comment"}, {"content": "another comment"}]
     }
     ...
]}

任何帮助将不胜感激。

【问题讨论】:

  • 您使用哪个库来生成 JSON?请添加示例 sn-p。
  • 它是默认的phoenix库,毒药:Poison.encode! %{posts: Blog.Repo.all(Blog.Post)}
  • 我假设你得到一个错误,对吗?你能把错误贴在这里吗?
  • 我的错误信息如下所示:** (Poison.EncodeError) unable to encode value: {Ecto.Associations.HasMany.Proxy, #Ecto.Associations.HasMany&lt;[name: :comments, target: Blog.Post, associated: Blog.Comment, references: :id, foreign_key: :post_id]&gt;}

标签: json elixir phoenix-framework ecto


【解决方案1】:

哎呀,目前没有简单的解决方案。我会尝试类似:

defimpl Poison.Encoder, for: Tuple do
  def encode(proxy, options) do
    Poison.Encoder.List.to_json(proxy.all, options)
  end
end

我们基本上是为接收上述代理并对所有项目进行编码的元组实现编码器。我们需要为此讨论更好的解决方案。

【讨论】:

  • 我不确定我是否完全理解你在做什么......你能否举个例子来说明你会如何做到这一点。
  • 这是对 Ecto 无法呈现关联的一种破解。您应该将它添加到您的项目文件之一,这应该足够了,除非我遗漏了什么,但上面代码中的代理是 post.comments 返回的值。理由是关联代理是一个元组,所以您正在教 Poison 如何将它们呈现为 JSON。
  • 我尝试将其添加到我的项目并收到以下警告:warning: undefined protocol function encode/2 (for protocol Poison.Encoder),在我尝试运行 Poison.encode! Blog.Repo.all(Blog.Post) 后收到此错误:** (UndefinedFunctionError) undefined function: Poison.Encoder.Tuple.encode/2 (blog) Poison.Encoder.Tuple.encode({Ecto.Associations.HasMany.Proxy, #Ecto.Associations.HasMany&lt;[name: :comments, target: Blog.Post, associated: Blog.Comment, references: :id, foreign_key: :post_id]&gt;}, [])
  • 抱歉,我已经更新了示例。它应该使用encode 而不是to_json
猜你喜欢
  • 2016-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 2020-05-04
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多