解码嵌套的JSON结构

时间:2017-03-14 14:03:09

标签: json elm

我尝试使用elmplayground的源代码,并尝试为博客创建配置json文件。我此时遇到的问题是,我不知道如何将帖子/页面作者解码为嵌套结构。我想要的是帖子和页面中的author字段引用config.json中的作者。

config.json:

{
  "posts": [{
    "slug": "/hello-world",
    "title": "Hello World",
    "name": "hello-world",
    "publishedDate": "2016-10-30",
    "author": "Gabriel",
    "intro": ""
  }],
  "pages": [{
    "slug": "/hello",
    "name": "index",
    "title": "Elm Playground",
    "publishedDate": "2016-09-01",
    "author": "Gabriel",
    "intro": ""
  }],
  "authors": {
    "Gabriel": {
      "name": "Gabriel Perales",
      "avatar": "..."
    }
  }
}

为网页和帖子输入内容:

type alias Content =
    { title : String
    , name : String
    , slug : String
    , publishedDate : Date
    , author : Author
    , markdown : WebData String
    , contentType : ContentType
    , intro : String
    }

输入作者:

type alias Author =
    { name : String
    , avatar : String
    }

目前这是我的配置解码器:

configDecoder : Decoder Config
configDecoder =
    Decode.map2 Config
        (field "posts" <| Decode.list <| decodeContent Post)
        (field "pages" <| Decode.list <| decodeContent Page)

decodeContent : ContentType -> Decoder Content
decodeContent contentType =
    Decode.map8 Content
        (field "slug" string)
        (field "name" string)
        (field "title" string)
        (field "publishedDate" decodeDate)
        (field "author"
            (string
                -- I want to decode the author from "authors"
                -- I have tried with 
                -- (\name -> at ["authors", name] decodeCofigAuthor) but it doesn\'t work
                |> andThen (\name -> Decode.succeed <| Author name "...")
            )
        )
        (Decode.succeed RemoteData.NotAsked)
        (Decode.succeed contentType)
        (field "intro" string)


decodeConfigAuthor : Decoder Author
decodeConfigAuthor =
    Decode.map2 Author
        (field "name" string)
        (field "avatar" string)

1 个答案:

答案 0 :(得分:3)

我首先解码作者,然后使用andThen将作者Dict传递给decodeContent。然后,您可以使用Decode.map将作者姓名转换为authors Dict中的查找。

decoder =
    (field "authors" <| Decode.dict <| authorDecoder)
        |> Decode.andThen configDecoder

configDecoder authors =
    Decode.map2 Config
        (field "posts" <| Decode.list <| decodeContent Post authors)
        (field "pages" <| Decode.list <| decodeContent Page authors)

decodeContent contentType authors =
    Decode.map8 Content
        -- …
        (field "author" (string |> Decode.map (\name -> Dict.get name authors)))
        -- …

这会将您的模型更改为使用Maybe Author,但如果Decode.andThen返回Decode.fail,您也可以使用Dict.get并返回Nothing