Elm JSON解码为对象列表:BadBody错误

问题描述

我对elm来说是超级新手,目前在解析HTTP响应(以JSON形式)时遇到问题。看起来像这样:

[
{
    "title":"Songs,Merry and Sad","author":"John Charles McNeill"
},{
    "title":"The Ambassadors","author":"Henry James"
}
]

基本上,我的Web应用程序向后端发送了一个请求,该请求以这样的JSON文件进行响应。我创建了此类型别名:

type alias Book =
    { title : String,author : String}

此外,这是我的HTTP请求:

search : Model -> Cmd Msg
search model =
  Http.post
    { url = "http://0.0.0.0:5000/search",body = Http.multipartBody [Http.stringPart "keyword" model.keyword],expect = Http.expectJson GotSearch searchDecoder
    }

这是我的解码器:

searchDecoder : Decoder (List Book)
searchDecoder =
    Json.Decode.list bookDecoder

bookDecoder : Decoder Book
bookDecoder =
    map2 Book
        (field "title" string)
        (field "author" string)

尽管在我的HTTP请求中明确说明了ExpectJSON,但出现此BadBody错误显示以下消息:

Problem with the value at json[0]:\n\n    {\n        \"title\": \"Songs,Merry and Sad\",\n        \"author\": \"John Charles McNeill\"\n    }\n\nExpecting a STRING

我不确定这是怎么回事。非常感谢您的帮助,因为我对此深感困惑。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)