gpt4 book ai didi

http - 如何从响应中提取多个键的数据。在榆树

转载 作者:可可西里 更新时间:2023-11-01 16:36:02 29 4
gpt4 key购买 nike

我试图从 API 响应中提取数据,但在 decodeData 中我只能传递一个解码字段,如果我试图传递多个字段则会出错

Function get is expecting the 2nd argument to be:

Decode.Decoder a
    type Msg = 
FindData |
ReqCbk ( Result Http.Error String )

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
FindData ->
( model, getData model.topic )

ReqCbk ( Ok newData) ->
( { model | firstName = newData, err = ""}, Cmd.none )

ReqCbk ( Err r ) ->
( { model | err = (toString r)}, Cmd.none )

getData : String -> Cmd Msg
getData topic =
let
url =
"http://localhost:9191/"++topic++"/v1/xyz"

request =
Http.get url decodeData
in
Http.send ReqCbk request

decodeData : Decode.Decoder String
decodeData =
Decode.field "firstName" Decode.string

使用这种方法我只能更新 firstName 但我想更新完整的用户数据。

API response is

{
firstName : "user",
lastName : "hero",
gender : "male"
}

最佳答案

如果数据定义为

type alias Data =
{ firstName : String
, lastName : String
, gender : String
}

然后你可以这样定义你的解码器:

decodeData : Decode.Decoder Data
decodeData =
Decode.map3 Data
(Decode.field "firstName" Decode.string)
(Decode.field "lastName" Decode.string)
(Decode.field "gender" Decode.string)

关于http - 如何从响应中提取多个键的数据。在榆树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48879472/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com