简体   繁体   English

使用Data.Aeson动态解析json

[英]Parse json dynamically with Data.Aeson

I follow docs and try: 我遵循文档并尝试:

let st = do result <- decode "{\"name\":\"Dave\",\"age\":2}" --bss
            flip parseMaybe result $ \obj -> do
                     name <- obj .: "name"
                     return name

I get: 我得到:

No instance for (FromJSON b0) arising from a use of .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of 没有使用(.JSON)的实例(FromJSON b0) .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of -obj。: .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of -obj。: .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of ($)', namely `\\ obj -> do { name <- obj .: "name"; .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of ($) .:' In a stmt of a 'do' block: name <- obj .: "name" In the expression: do { name <- obj .: "name"; return name } In the second argument of ,即`\\ obj-> do {name <-obj。:“ name”; return name }' 返回名称}'

How to do it properly? 怎么做呢? What am I doing wrong? 我究竟做错了什么?

FromJSON b0 indicates that the type isn't fixed at this point. FromJSON b0指示此时类型尚未固定。 If you, however, fix the type, for example to String , it will work: 但是,如果您将类型固定为例如String ,它将起作用:

let st = do result <- decode "{\"name\":\"Dave\",\"age\":2}"
            flip parseMaybe result $ \obj -> do
                     name <- obj .: "name"
                     return (name :: String)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM