获取 json 值并使用某些特定模式将其存储在我的 table 中

get json values and store that in my table with some specific schema

如何获取特定的 json 值并将其存储在我的 table 中以及一些特定的模式。

例如我试过

def save_to_payment(conn,result) do
  Poison.decode!(result, as: %Payments.payment{} )
end

使用此架构,

schema "payment" do
  field :payment_id, :string
  field :state, :string
  field :amount, :decimal
  timestamps()
end

然而实际 Json 有很多字段,我只需要其中的几个..

我需要将我想要的字段映射到模式,以便将它们保存在数据库中

我只是将原始解码的 Map 传递给 Payment.changeset/2 并让它处理删除额外的字段并在需要时进行类型转换:

json = "{\"state\":\"CA\",\"payment_id\":1,\"go\":\"here\",\"extra\":\"fields\",\"amount\":123}"
decoded = Poison.decode!(json)
changeset = Payment.changeset(%Payment{}, decoded)
# You can now `Repo.insert!(changeset)` or use it in forms etc.