让一些json用aeson解析Haskell有些困难。非常感谢帮助。这是我试图解析的JSON:
{"dailygameschedule":{"lastUpdatedOn":"2017-12-10 1:05:34 PM","gameentry":
[{"id":"34567","scheduleStatus":"Normal","originalDate":null,"originalTime":null,"delayedOrPostponedReason":null,"date":"2017-01-18","time":"7:00PM","awayTeam":{"ID":"81","City":"Toronto","Name":"Raptors","Abbreviation":"TOR"},"homeTeam":{"ID":"85","City":"Philadelphia","Name":"76ers","Abbreviation":"PHI"},"location":"Wells Fargo Center"},
....
,{"id":"34575","scheduleStatus":"Normal","originalDate":null,"originalTime":null,"delayedOrPostponedReason":null,"date":"2017-01-18","time":"10:30PM","awayTeam":{"ID":"87","City":"Indiana","Name":"Pacers","Abbreviation":"IND"},"homeTeam":{"ID":"103","City":"Sacramento","Name":"Kings","Abbreviation":"SAC"},"location":"Sleep Train Arena"}]}}
我是如何解析它的:
module Models.Games where
import Control.Monad
import Data.Aeson
import Data.Time.Clock
newtype GameList = GameList [GameEntry] deriving (Show, Eq)
data GameEntry = GameEntry { eid :: Integer
, scheduleStatus :: String
, date :: UTCTime
, time :: UTCTime
, awayTeam :: Team
, homeTeam :: Team
, location :: String
} deriving (Show, Eq)
data Team = Team { tid :: Integer
, city :: String
, name :: String
, abbreviation :: String
} deriving (Show, Eq)
instance FromJSON GameList where
parseJSON (Object o) =
GameList <$> (o .: "gameentry")
parseJSON _ = mzero
instance FromJSON GameEntry where
parseJSON (Object o) =
GameEntry <$> (o .: "id")
<*> (o .: "scheduleStatus")
<*> (o .: "date")
<*> (o .: "time")
<*> (o .: "awayTeam")
<*> (o .: "homeTeam")
<*> (o .: "location")
parseJSON _ = mzero
instance FromJSON Team where
parseJSON (Object o) =
Team <$> (o .: "ID")
<*> (o .: "City")
<*> (o .: "Name")
<*> (o .: "Abbreviation")
parseJSON _ = mzero
来电者(在另一档案中):
....
import Network.HTTP.Simple
...
getGamesAPI :: IO GameList
getGamesAPI = do
un <- username
pw <- password
let encoded = encode . fromString $ un ++ ":" ++ pw
authstr = (fromString "Basic ") `mappend` encoded
request = setRequestHeaders [ ("Accept-Encoding", "gzip")
, ("Authorization", authstr)
]
$ fullSeasonGames
response <- httpJSON request
return $ getResponseBody response
然而,当我尝试运行它时,我最终会得到类似的东西:
...("homeTeam",Object (fromList [("Abbreviation",String "GSW"),("City",String "Golden State"),("Name",String "Warriors"),("ID",String "101")])),("id",String "34574"),("delayedOrPostponedReason",Null)]),Object (fromList [("time",String "10:30PM"),("location",String "Sleep Train Arena"),("originalDate",Null),("awayTeam",Object (fromList [("Abbreviation",String "IND"),("City",String "Indiana"),("Name",String "Pacers"),("ID",String "87")])),("scheduleStatus",String "Normal"),("date",String "2017-01-18"),("originalTime",Null),("homeTeam",Object (fromList [("Abbreviation",String "SAC"),("City",String "Sacramento"),("Name",String "Kings"),("ID",String "103")])),("id",String "34575"),("delayedOrPostponedReason",Null)])]),
...("lastUpdatedOn",String "2017-12-10 1:05:34 PM")]))]),
responseCookieJar = CJ {expose = [Cookie {cookie_name = "JSESSIONID", cookie_value = "9E7F8296FEC1EF68F6827F411D9AA1B9",
cookie_expiry_time = 3017-04-12 00:00:00 UTC, cookie_domain = "api.mysportsfeeds.com", cookie_path = "/leaguemanager-web",
cookie_creation_time = 2017-12-10 18:12:56.681505 UTC, cookie_last_access_time = 2017-12-10 18:12:56.681505 UTC,
cookie_persistent = False, cookie_host_only = True, cookie_secure_only = False, cookie_http_only = True}]},
responseClose' = ResponseClose}) "key \"gameentry\" not present"
我对此消息感到有点困惑,因为它似乎在它之前解析得很好?我尝试通过几种方式重构我的数据类型以处理"key \"gameentry\" not present"
消息,但没有这样的运气。