我定义了一个简单的serverResponse
,用于模拟从API获取数据并在elm-lang中解析它。
我遇到的问题是如何将Result
转换为显示信息的HTML代码!
最好的方法是什么?
import String exposing (..)
import String exposing (..)
import List exposing (..)
import Result exposing (map)
import Json.Decode as Json exposing (..)
type alias Team =
{ department : String
, names: List String
}
serverResponse =
"""
[{"department":"product","names":["bob","sally","george"]},{"department":"marketing","names":["billy","diane","anita"]},{"department":"sales","names":["howard","steve","isha"]}]
"""
stringDecoder =
Json.list Json.string
infoDecoder : Json.Decoder Team
infoDecoder =
Json.map2 Team
(Json.field "department" Json.string)
(Json.field "names" stringDecoder)
teamDecoder : Json.Decoder (List Team)
teamDecoder =
Json.list infoDecoder
toList team =
p [] [
team.department
]
transformList teams =
toList teams
main =
Json.decodeString teamDecoder serverResponse
|> toString
|> text