Advertisement
Guest User

Untitled

a guest
Apr 30th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. {-# LANGUAGE OverloadedStrings #-}
  3.  
  4. import Data.Aeson
  5. import qualified Data.ByteString.Lazy.Char8 as C
  6. import Control.Monad
  7. import Control.Applicative
  8.  
  9.  
  10.  
  11. data AuctionInfo = AuctionInfo {
  12.     realm :: Realm ,
  13.     alliance :: Auctions  ,
  14.     horde ::  Auctions  ,
  15.     neutral :: Auctions  
  16. }   deriving (Show )
  17.  
  18. instance FromJSON AuctionInfo where
  19.    parseJSON (Object o) = do
  20.       r <- o .: "realm" >>= parseJSON
  21.       a <- o .: "alliance" >>= parseJSON
  22.       h <- o .: "horde" >>= parseJSON
  23.       n <- o .: "neutral" >>= parseJSON
  24.       return $  AuctionInfo r a h n
  25.    parseJSON _ = mzero
  26.    
  27.  
  28.  
  29. data Realm = Realm { name2 :: String , slug:: String} deriving (Show )
  30. instance FromJSON Realm where
  31.       parseJSON (Object o) = Realm <$>
  32.             o .: "name" <*>
  33.             o .: "slug"
  34.       parseJSON _ = mzero
  35.  
  36. data Auctions = Auctions {auctions :: [Auc]} deriving (Show)
  37. instance FromJSON Auctions where  
  38.       parseJSON (Object o ) = Auctions <$> o.: "auctions"
  39.       parseJSON _ = mzero
  40.  
  41. data Auc = Auc {
  42.     auc :: Integer,
  43.     itme :: Int,
  44.     owner ::  String,
  45.     bid :: Integer,
  46.     buyout ::Integer,
  47.     quantity :: Int,
  48.     timeLeft :: String,
  49.     rand :: Integer,
  50.     seed :: Integer
  51.     } deriving (Show )
  52.  
  53. instance FromJSON Auc where
  54.       parseJSON (Object o ) = Auc <$>
  55.             o .: "auc" <*>
  56.             o .: "item" <*>
  57.             o .: "owner" <*>
  58.             o .: "bid" <*>
  59.             o .: "buyout" <*>
  60.             o .: "quantity" <*>
  61.             o .: "timeLeft" <*>
  62.             o .: "rand" <*>
  63.             o .: "seed"
  64.       parseJSON _ = mzero
  65.  
  66.  
  67. main = do
  68.     au<- C.readFile "a.json"
  69.     let x = decode au :: Maybe AuctionInfo
  70.     case x of
  71.         Just a -> do
  72.             {-putStrLn.show $ a-}
  73.             putStrLn .show.length.auctions.alliance $ a
  74.             putStrLn "ok"
  75.         Nothing -> putStrLn "fail"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement