给出对象的JSON列表,例如:
async function loadCommand(command) {
let TestCommand = await import("./commands/TestCommand.js");
let test = new TestCommand();
console.log(test.getName());
}
如何使用[{"id":"1", "name":"Jane"},{"id":"2", "name":"Joe"}]
作为键将其解码为Dict String Foo
,其中id
是类型Foo
的记录? (请注意,记录中还包含ID。)
答案 0 :(得分:2)
例如使用以下组合:
{id: String, name: String}
(https://package.elm-lang.org/packages/elm/json/latest/Json-Decode#list Json.Decode.list
(https://package.elm-lang.org/packages/elm/json/latest/Json-Decode#map2)Json.Decode.map2
(https://package.elm-lang.org/packages/elm/core/latest/Dict#fromList)Dict.fromList
(https://package.elm-lang.org/packages/elm/core/latest/Tuple#pair)Tuple.pair