我正在尝试FromJSON
为 Aeson 编写一个函数。
JSON:
{
"total": 1,
"movies": [
{
"id": "771315522",
"title": "Harry Potter and the Philosophers Stone (Wizard's Collection)",
"posters": {
"thumbnail": "http://content7.flixster.com/movie/11/16/66/11166609_mob.jpg",
"profile": "http://content7.flixster.com/movie/11/16/66/11166609_pro.jpg",
"detailed": "http://content7.flixster.com/movie/11/16/66/11166609_det.jpg",
"original": "http://content7.flixster.com/movie/11/16/66/11166609_ori.jpg"
}
}
]
}
ADT:data Movie = Movie {id::String, title::String}
我的尝试:
instance FromJSON Movie where
parseJSON (Object o) = do
movies <- parseJSON =<< (o .: "movies") :: Parser Array
v <- head $ decode movies
return $ Movie <$>
(v .: "movies" >>= (.: "id") ) <*>
(v .: "movies" >>= (.: "title") )
parseJSON _ = mzero
这给了Couldn't match expected type 'Parser t0' with actual type 'Maybe a0' In the first argument of 'head'
.
如您所见,我正在尝试选择 中的第一部电影Array
,但我也不介意获取电影列表(如果数组中有多个电影)。