简体   繁体   中英

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 .:' 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"; return name }'

How to do it properly? What am I doing wrong?

FromJSON b0 indicates that the type isn't fixed at this point. If you, however, fix the type, for example to String , it will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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