【问题标题】:Decode nested variable-length JSON in ELM在 ELM 中解码嵌套的可变长度 JSON
【发布时间】:2017-08-25 15:28:39
【问题描述】:

我无法在 ELM 中解码一个有点复杂的 JSON 响应。 JSON 响应结构(我无法控制)与我的应用程序的数据树/模型不匹配。

这个 JSON 的解码器代码是什么样的?

我正在使用 NoRedInk 的 Json.Decode.Pipeline - http://package.elm-lang.org/packages/NoRedInk/elm-decode-pipeline/3.0.0/Json-Decode-Pipeline

我的项目的 Github 存储库 - https://github.com/areai51/my-india-elm

JSON 响应 - https://data.gov.in/node/85987/datastore/export/json

代码

 type alias Leader =
    { attendance : Float      <------- Logic = (Sessions Attended / Total Sessions) * 100
    , name : String
    , state : String
    }


type alias Model =
    { leaders : WebData (List Leader)
    }


initialModel : Model
initialModel =
    { leaders = RemoteData.Loading
    }

JSON 请注意,“数据”数组中没有我可以直接映射到的键。

{
   "fields":[   // Definitions for the "data" key
      {
         "id":"a",
         "label":"S.No.",
         "type":"string"
      },
      {
         "id":"b",
         "label":"Division\/Seat No.",
         "type":"string"
      },....
    ],
   "data":[      <------- Leaders
      [
         1,
         3,
         "Smt.  Sonia  Gandhi",      <------- Name
         15,
         12,
         "Uttar Pradesh",      <------- State
         "Rae Barelii",
         20,      <------- Total Sessions (Attendance)
         9      <------- Sessions Attended (Attendance)
      ],
      [
         2,
         15,
         "Shri  Dayanidhi  Maran",      <------- Name
         15,
         12,
         "Tamil Nadu",      <------- State
         "Chennai Central",
         20,      <------- Total Sessions (Attendance)
         7      <------- Sessions Attended (Attendance)
      ],
      [
         3,
         16,
         "Shri  A.  Raja",      <------- Name
         15,
         12,
         "Tamil Nadu ",      <------- State
         "Nilgiris",
         20,      <------- Total Sessions (Attendance)
         16      <------- Sessions Attended (Attendance)
      ],.....
    ]
}

更新的解决方案

leadersDecoder : Decode.Decoder (List Leader)
leadersDecoder =
    Decode.at [ "data" ] (Decode.list leaderDecoder)


leaderDecoder : Decode.Decoder Leader
leaderDecoder =
    let
        sessionsAttendedDecoder =
            Decode.index 7 Decode.float
                |> Decode.andThen (\total -> attendanceDecoder
                |> Decode.map (\attended -> (attended / total) * 100))
    in
        Decode.map3 Leader
            sessionsAttendedDecoder
            (Decode.index 2 Decode.string)
            (Decode.index 5 Decode.string)

attendanceDecoder : Decode.Decoder Float
attendanceDecoder =
    (Decode.oneOf
        [ Decode.index 8 Decode.float
        , Decode.succeed 0
        ]
    )

【问题讨论】:

    标签: javascript json decode elm


    【解决方案1】:

    Json.Decode.index 可用于从具有指定解码器的数组中提取特定索引。将其与andThenmap 结合起来进行基于多个字段的考勤计算:

    import Json.Decode exposing (..)
    
    leaderDecoder : Decoder Leader
    leaderDecoder =
        let
            sessionsAttendedDecoder =
                index 7 float
                    |> andThen (\total -> index 8 float
                    |> map (\attended -> (attended / total) * 100))
        in
            map3 Leader
                sessionsAttendedDecoder
                (index 2 string)
                (index 5 string)
    

    【讨论】:

    • 如果我想在更新模型之前按“出席”键对响应进行排序,我该怎么做?
    • Stack Overflow 问题最好留给单一的、有针对性的问题。请为排序问题打开一个新问题,并包含来自您的 update 函数的代码。
    • 不确定我是否应该在这里发布.. 但你能帮我解决我的新问题link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2017-01-25
    • 1970-01-01
    • 2018-07-22
    • 2018-04-10
    • 2018-08-30
    • 2015-11-30
    相关资源
    最近更新 更多