json - 解码嵌套 JSON 结构

标签 json elm

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

配置.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)

最佳答案

我首先对作者进行解码,然后使用 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

关于json - 解码嵌套 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788100/

相关文章:

Elm - 映射列表并将字符串附加到每个项目的末尾

list - Elm 在更新列表中添加项目

go - 当参数与工作示例相同时,为什么 Elm 不会为 JSON 数据示例编译这个 HTTP 请求?

javascript - 如何从 getRequest 获取 JSON

java - 如何将日期转换为json

json - 如何通过 jq 命令将 json 文件中的所有整数转换为字符串?

elm - 为什么我不能在 Elm REPL 的无限循环中从 Debug.log 获得输出?

javascript - Elm:从 Browser.element 切换到 Browser.application - JS 方面需要进行哪些更改?

json - 如何在 Spring Boot 中覆盖默认的 JSON 响应

javascript - 使用 Javascript 从 PHP 获取 JSON 数据 :