我正在尝试在IO中进行JSON解析:
{-# 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"
I get the error:
/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 <$> response .: "name"
的结果绑定到IO
中,但它只是一个Parser
值。我对 Aeson 不是很熟悉,但我认为你需要使用Data.Aeson.Types.parse
或parseMaybe
来运行解析器(纯粹地)。 - Jon Purdy