2
我需要解析不具有嚴格的結構JSON API響應:埃宋解析動態對象
{
response: { /* any object here */ }
}
怎麼可能寫出parseResponse
將離開解析能力(或爲它選擇一個分析器)供以後使用?
我最後一次嘗試如下。我不喜歡它,因爲它不允許選擇響應類型,如Aeson的decode
所做的那樣。
data APIResponse =
APIResponse { response :: Value } deriving (Show,Generic)
instance FromJSON APIResponse
parseResponse :: BC.ByteString -> Either String Value
parseResponse resp =
case eitherDecode . BLC.fromStrict $ resp of
Right x -> Right $ response x
Left msg -> Left msg
大運行產生的解析器,謝謝!請問我可以指點Data.Aeson的文檔是否應該查找將Value轉換爲Array [Value]的方法? –
查看'parse' /'parseMaybe' /'parseEither'系列。具體的部分是[解碼混合類型對象](http://hackage.haskell.org/package/aeson-0.7.0.2/docs/Data-Aeson.html)。你也可以在'Value'上模式匹配,並使用'vector'操作來解開'Array'。 –