将函数提升到IO monad中以解析从文件中读取的json字符串

时间:2012-10-04 21:47:30

标签: haskell

我遇到的问题是readFile会返回IO String,但解析需要定期String(或者在ByteString下面的示例中)。我虽然只使用liftM,但下面的示例仍然失败。有什么问题?

import Data.Aeson
import Data.Attoparsec
import Data.ByteString (ByteString, pack)
import Data.Maybe
import Network.HTTP
import Network.URI
import qualified Data.ByteString.Char8 as C
import Control.Monad

main = do
    myres  <-  liftM parse json (C.readFile "dummy.json")
    print myres

ERROR:

Couldn't match expected type `t0 -> Parser a0'
            with actual type `Parser Value'
In the second argument of `liftM', namely `json'
In a stmt of a 'do' block:
  myres <- liftM parse json (C.readFile "dummy.json")
In the expression:
  do { myres <- liftM parse json (C.readFile "dummy.json");
       print myres }

1 个答案:

答案 0 :(得分:5)

您错过(parse json)周围的括号,因此liftM已应用于parse