haskell – 使用Aeson嵌套JSON中的Parse Array

我正在为Aeson写一个From JSON函数.

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

这给出了无法匹配预期类型’Parser t0’与实际类型’也许a0’在’头’的第一个参数.

正如你所看到的,我正在尝试选择Array中的第一部电影,但是我也不会介意获得电影列表(如果Array中有几个).

解决方法@H_502_18@
如果您真的想从JSON数组电影解析单个影片,您可以执行以下操作:
instance FromJSON Movie where
    parseJSON (Object o) = do
        movieValue <- head <$> o .: "movies"
        Movie <$> movieValue .: "id" <*> movieValue .: "title"
    parseJSON _ = mzero

但更安全的路线是通过newtype包装解析[Movie]:

main = print $movieList <$> decode "{\"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\"}}]}"

newtype MovieList = MovieList {movieList :: [Movie]}

instance FromJSON MovieList where
    parseJSON (Object o) = MovieList <$> o .: "movies"
    parseJSON _ = mzero

data Movie = Movie {id :: String,title :: String}

instance FromJSON Movie where
    parseJSON (Object o) = Movie <$> o .: "id" <*> o .: "title"
    parseJSON _ = mzero

相关文章

事件冒泡和事件捕获 起因:今天在封装一个bind函数的时候,发现el.addEventListener函数支持第三个参数...
js小数运算会出现精度问题 js number类型 JS 数字类型只有number类型,number类型相当于其他强类型语言...
什么是跨域 跨域 : 广义的跨域包含一下内容 : 1.资源跳转(链接跳转,重定向跳转,表单提交) 2.资源...
@ &quot;TOC&quot; 常见对base64的认知(不完全正确) 首先对base64常见的认知,也是须知的必须有...
搞懂:MVVM模式和Vue中的MVVM模式 MVVM MVVM : 的缩写,说都能直接说出来 :模型, :视图, :视图模...
首先我们需要一个html代码的框架如下: 我们的目的是实现ul中的内容进行横向的一点一点滚动。ul中的内容...