[英]Decoding errors in elm 0.19
I am new to elm and I having a very hard time parsing a json from html to elm and using it. 我是elm的新手,我很难将json从html解析为elm并使用它。
This is what I am trying to do: 这就是我想要做的:
In my html doc: 在我的HTML文档中:
var app = Elm.Main.init({
node: document.getElementById("app"),
flags: questions
});
then in elm: 然后在榆树:
main =
Browser.element { init = init, update = update, subscriptions = subscriptions, view = view }
-- MODEL
type alias Model =
{
questionsToAnswer: List QuestionToAnswer
, currentQuestion: Int
, initializeGame: Bool
}
type alias QuestionToAnswer =
{
question: String
, a: String
, b: String
, c: String
, d: String
, answer: String
}
questionDecoder : Decoder QuestionToAnswer
questionDecoder =
map6 QuestionToAnswer
(field "question" string)
(field "A" string)
(field "B" string)
(field "C" string)
(field "D" string)
(field "answer" string)
init : Json.Decode.Value -> (Model, Cmd Msg)
init questions =
(Model (getQuestions questions) 0 True, Cmd.none)
getQuestions : Json.Decode.Value -> List QuestionToAnswer
getQuestions questions =
case(decodeValue questionDecoder questions) of
Ok question ->
[question]
_ ->
[ QuestionToAnswer "me" "me" "me" "me" "me" "me"]
My json looks like this: 我的json看起来像这样:
{
"question": "In mobster lingo, if you 'sing like a canary' what are you doing?",
"A": "impersonating Sinatra",
"B": "talking to the cops",
"C": "killing an enemy",
"D": "betting on horses",
"answer": "B"
}
I am outputting all the response as debug.toString in the view just to see what is happening because I also don't know how to log the error that was being produced before. 我在视图中输出所有响应作为debug.toString只是为了看看发生了什么,因为我也不知道如何记录之前生成的错误。 The compiler says my call to getQuestions questions produces a Json.Decode.error which I find difficult to believe because I think everything looks ok to me. 编译器说我对getQuestions问题的调用会产生一个Json.Decode.error,我觉得很难相信,因为我觉得一切看起来都不错。
The error: 错误:
"{\\"question\\":\\"In mobster lingo, if you 'sing like a canary' what are you doing?\\",\\"A\\":\\"impersonating Sinatra\\",\\"B\\":\\"talking to the cops\\",\\"C\\":\\"killing an enemy\\",\\"D\\":\\"betting on horses\\",\\"answer\\":\\"B\\"}" Expecting an OBJECT with a field named
question
“{\\”问题\\“:\\”在流氓行话中,如果你像金丝雀一样'你在做什么?',“A”:“模仿Sinatra”,“B”: “与警察交谈”,“C”:“杀死一个敌人”,“D”:“投注马匹”,“回答”:“\\”B \\“}”期待带有名为question
的字段的OBJECT
It looks like you're passing a string in as flags
, not a JSON object. 看起来你正在将字符串作为flags
传递,而不是JSON对象。 If so, you can do one of two things: 如果是这样,你可以做以下两件事之一:
1: Parse the JSON string on the JavaScript side, before passing it to Elm via flags
: 1:在通过flags
传递给Elm之前,在JavaScript端解析JSON字符串:
var app = Elm.Main.init({
node: document.getElementById("app"),
flags: JSON.parse(questions)
});
The downside to this approach is that if an error happen during parsing it happens on the JavaScript-side and has to be handled there. 这种方法的缺点是,如果在解析过程中发生错误,它将发生在JavaScript端,并且必须在那里处理。 If you need to deal with the error in Elm, you have to pass a more complex structure to flags
that can represent both errors and success. 如果需要处理Elm中的错误,则必须将更复杂的结构传递给可以表示错误和成功的flags
。
2: Use decodeString
instead of decodeValue
, and change the type of init
and getQuestions
accordingly: 2:使用decodeString
而不是decodeValue
,并相应地更改init
和getQuestions
的类型:
init : String -> (Model, Cmd Msg)
init questions =
(Model (getQuestions questions) 0 True, Cmd.none)
getQuestions : String -> List QuestionToAnswer
getQuestions questions =
case (decodeString questionDecoder questions) of
Ok question ->
[question]
_ ->
[ QuestionToAnswer "me" "me" "me" "me" "me" "me"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.