我正在编写一个使用Github Webhooks API的应用程序。 在钩子消息中,我得到了这个JSON结构:http://organicorange.ro:8000/set
我正在做类似的声明:
newtype CommitList = CommitList {commitList :: [Commit]}
instance FromJSON CommitList where
parseJSON (Object o) = CommitList <$> o .: "commits"
parseJSON _ = mzero
data Commit = Commit {ids :: String, message :: String, url :: String, modified :: [String], author :: Auth} deriving (Show)
instance FromJSON Commit where
parseJSON (Object o) = Commit <$> o .: "id" <*> o .: "message" <*> o .: "url" <*> o .: "modified" <*> o .: "author"
parseJSON _ = mzero
data Auth = Auth {name :: String, email :: String, username :: String} deriving (Show)
instance FromJSON Auth where
parseJSON (Object o) = Auth <$> o .: "name" <*> o .: "email" <*> o .: "username"
parseJSON _ = mzero
我如何解析&#34;修改过的&#34;数组返回一个列表?
答案 0 :(得分:1)
我真的不确定这是否就是你要问的问题,但是如果你问的话,我可以在给出样本JSON的情况下获取所有修改文件的列表&#34 ;,那么这应该有效:
main = do
-- get the JSON from the api
json <- simpleHttp "http://organicorange.ro:8000/set"
-- parse and pull out the commits into [Commit], if the parse fails then you will just have am empty list
let commits = maybe ([]) (commitList) (decode json :: Maybe CommitList)
-- for each commit, pull out the modified files and then concatenate the results together
print $ concatMap (modified) commits