我試圖做的JSON解析在IO:哈斯克爾埃宋:如何獲取價值解析器的IO單子
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Simple
import Data.Aeson
import Data.Maybe (fromJust)
main :: IO()
main = do
response <- getResponseBody <$> httpJSON "http://localhost:9200" :: IO Object
name <- fromJust <$> response .: "name" :: Parser String
print "hi"
我得到的錯誤:
/home/nut/dev/haskell/elastic/app/Main.hs:39:11: error:
• Couldn't match type ‘Parser’ with ‘IO’
Expected type: IO String
Actual type: Parser String
• In a stmt of a 'do' block:
所以,我該怎麼辦從json結果中獲得name
?
它看起來像你想的'fromJust <$>響應結果綁定: 「名字」 在' 'IO',但它只是一個'Parser'值。我對Aeson並不是很熟悉,但我認爲你需要使用'Data.Aeson.Types.parse'或'parseMaybe'運行解析器(純粹)。 –