0

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

「変更された」配列を解析してリストを返すにはどうすればよいですか?

4

1 に答える 1

1

これがあなたの求めているものかどうかはよくわかりませんが、「そのサンプル JSON を指定して、変更されたすべてのファイルのリストを取得するにはどうすればよいですか」と尋ねている場合、これは機能するはずです

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
于 2014-08-30T00:08:28.843 に答える