我想獲得一個配置數據記錄了下面的代碼來獲取對象了JSON的:嘗試使用埃宋
data Connections = Connections { cfgProperty :: !Object
, connectionName :: String
} deriving (Show, Generic)
data Config = Config {connections :: [Connections]} deriving (Show, Generic)
data Cfg = Cfg { config :: Config } deriving (Show, Generic)
instance FromJSON Cfg
instance FromJSON Config
instance FromJSON Connections
instance ToJSON Cfg
instance ToJSON Config
instance ToJSON Connections
jsonFile :: FilePath
jsonFile = "config/config.json"
getCfg :: IO B.ByteString
getCfg = B.readFile jsonFile
parseCfg = do
j <- (A.eitherDecode <$> getCfg) :: IO (Either String Cfg)
case j of
Left err -> liftIO $ putStrLn err
Right j -> config j
,我發現了以下錯誤:
/apps/workspace/hade/src/Actor/MasterActor.hs: 56, 20
• Couldn't match expected type ‘IO()’ with actual type ‘Config’
• In the expression: config j
In a case alternative: Right j -> config j
In a stmt of a 'do' block:
case j of {
Left err -> liftIO $ putStrLn err
Right j -> config j }
這裏是config.json
{
"config":
{
"connections": [
{
"cfgProperty":
{
"Env": "local",
"Host": "localhost",
"Port": "8001",
"Directory": "/apps/workspace/hade/maps/src01_cvs"
},
"connectionName": "src01_cvs"
},
{
"cfgProperty":
{
"Env": "local",
"Host": "localhost",
"Port": "8001",
"Directory": "/apps/workspace/hade/maps/trg01_cvs"
},
"connectionName": "trg01_cvs"
}
]
}
}{
"config":
{
"connections": [
{
"cfgProperty":
{
"Env": "local",
"Host": "localhost",
"Port": "8001",
"Directory": "/apps/workspace/hade/maps/src01_cvs"
},
"connectionName": "src01_cvs"
},
{
"cfgProperty":
{
"Env": "local",
"Host": "localhost",
"Port": "8001",
"Directory": "/apps/workspace/hade/maps/trg01_cvs"
},
"connectionName": "trg01_cvs"
}
]
}
}
我已經嘗試了許多不同的配置使用bothDecode和解碼,但我碰到路由器每次鎖定。我可以得到代碼打印出來的配置記錄,如果我改變的情況是:
case j of
Left err -> putStrLn err
Right j -> print $ config j
(連同一些其他的變化),但我不能把它簡單地返回配置記錄本身。任何援助將不勝感激。
這是一個提示:'case'語句的每個分支必須返回相同的類型。另外,'do'塊的類型與上一個表達式相同。 「parseCfg」的類型應該是什麼?如果您在所有頂級聲明中放置類型註釋(無論如何,您應該這樣做),那麼解決問題的方法應該會變得更加清晰。 –
是否返回$ config j工作? – mac10688
亞歷克西斯,我可以從「左 - >」返回一個空的Config記錄,以使類型相同,但我不知道我該怎麼做。有任何想法嗎?我嘗試了一些不同的東西,但是這一點沒有成功。 – peteofce