Elixir - Nested JSON parsing to structs

后端 未结 2 1916
太阳男子
太阳男子 2021-02-08 08:05

Disclaimer: I\'ve checked the question here and it does not answer mine.

I am trying to come up with a way for nested struct parsing of JSONs. Example:

{         


        
2条回答
  •  难免孤独
    2021-02-08 09:02

    I believe the above is now possible with Poison:

    defmodule User do
      @derive [Poison.Encoder]
      defstruct [:address]
    end
    
    defmodule Address do
      @derive [Poison.Encoder]
      defstruct [:street]
    end
    
    Poison.decode(response, as: %User{address: %Address{}})
    

提交回复
热议问题