我一直在砸我的腦袋,試圖用艾森解析Bitly的反應。 可能有人給我一個提示,什麼類型的Haskell應該定義 以及如何使用埃宋來然後解析以下爲那些類型?:你如何解析Aeson的BitSys響應JSON?
// BITLY EXPAND RESPONSE
{
"data": {
"expand": [
{
"global_hash": "900913",
"long_url": "http://google.com/",
"short_url": "http://bit.ly/ze6poY",
"user_hash": "ze6poY"
}
]
},
"status_code": 200,
"status_txt": "OK"
}
// BITLY SHORTEN RESPONSE
{
"data": {
"global_hash": "900913",
"hash": "ze6poY",
"long_url": "http://google.com/",
"new_hash": 0,
"url": "http://bit.ly/ze6poY"
},
"status_code": 200,
"status_txt": "OK"
}
這裏是我到目前爲止已經試過:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module BitlyClientResponses where
import Control.Applicative
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as L (pack)
import qualified Data.HashMap.Strict as M
data DataStatusCodeStatusTxt =
DSCST { ddata :: ResponseData
, status_code :: Integer
, status_txt :: String
}
deriving (Eq, Show)
data ResponseData
= ExpandResponseData { expand :: [Response]
}
deriving (Eq, Show)
data Response = ExpandResponse { long_url :: String -- URI
, global_hash :: String
, short_url :: String -- URI
, user_hash :: String
-- , hash :: [String]
-- , error :: String
}
| J String
| N String
deriving (Eq, Show)
instance FromJSON DataStatusCodeStatusTxt where
parseJSON (Object o) = DSCST <$>
o .: "data" <*>
o .: "status_code" <*>
o .: "status_txt"
parseJSON x = fail $ "FAIL: DataStatusCodeStatusTxt: " ++ (show x)
instance FromJSON ResponseData where
parseJSON (Object o) =
case M.lookup "expand" o of
-- LOST RIGHT HERE
Just v -> return $ ExpandResponseData [J ((show o) ++ " $$$ " ++ (show v))]
Nothing -> return $ ExpandResponseData [N "N"]
parseJSON x = fail $ "FAIL: ResponseData: " ++ (show x)
instance FromJSON Response where
parseJSON (Object o) = ExpandResponse <$>
o .: "long_url" <*>
o .: "global_hash" <*>
o .: "short_url" <*>
o .: "user_hash"
-- o .: "hash" <*>
-- o .: "error" <*>
parseJSON x = fail $ "FAIL: Response: " ++ (show x)
parseResponse :: String -> Either String DataStatusCodeStatusTxt
parseResponse x = eitherDecode $ L.pack x
當我輸入(手動編輯的可讀性):
"{ \"status_code\": 200,
\"status_txt\": \"OK\",
\"data\": { \"expand\": [
{ \"short_url\": \"http:\\/\\/bit.ly\\/LCJq0b\",
\"long_url\": \"http:\\/\\/blog.swisstech.net\\/2012\\/06\\/local-postfix-as-relay-to-amazon-ses.html\",
\"user_hash\": \"LCJq0b\",
\"global_hash\": \"LCJsVy\" }, ...
我回來(手動編輯過):
Right
(Right
(DSCST
{ddata = ExpandResponseData {expand = [J "fromList [(\"expand\",Array (fromList [Object fromList [(\"long_url\",String \"http://blog.swisstech.net/2012/06/local-postfix-as-relay-to-amazon-ses.html\"),(\"global_hash\",String \"LCJsVy\"),(\"short_url\",String \"http://bit.ly/LCJq0b\"),(\"user_hash\",String \"LCJq0b\")], ...
$$$
Array (fromList [Object fromList [(\"long_url\",String \"http://blog.swisstech.net/2012/06/local-postfix-as-relay-to-amazon-ses.html\"),(\"global_hash\",String \"LCJsVy\"),(\"short_url\",String \"http://bit.ly/LCJq0b\"),(\"user_hash\",String \"LCJq0b\")], ...
在代碼中尋找-- LOST RIGHT HERE
。我無法弄清楚如何解析"expand"
的數組。
這將是很高興看到如何取得進展。也許我在錯誤的道路上,有人可以直接設置我(例如,也許我迄今爲止定義的數據類型已關閉)。
你爲什麼將long_url和short_url定義爲[String]而不是純String? – sinelaw
感謝您的支持。我已經複製了ExpandRequest構造函數,您可以在其中提供多個URL。但是,正如你所指出的那樣,這些迴應應該只是「字符串」 - 很好!我更新了類型。也許這是我的問題的一部分(現在會嘗試) - 仍然需要研究@abrahamson的反應。 – haroldcarr