我試圖以一個API的調用解析爲使用埃宋庫Haskell的記錄類型解析複雜jsons與埃宋
我使用維基百科的頁面,並將其解析到標題和鏈接列表。 樣本會是這樣,
{"query":{"pages":{"6278041":{"pageid":6278041,"ns":0,"title":"Lass","links":[{"ns":0,"title":"Acronym"},{"ns":0,"title":"Dead Like Me"},{"ns":0,"title":"Donna Lass"},{"ns":0,"title":"George Lass"},{"ns":0,"title":"Girl"},{"ns":0,"title":"Lassana Diarra"},{"ns":0,"title":"Lightning Lass"},{"ns":0,"title":"Real Madrid"},{"ns":0,"title":"Shadow Lass"},{"ns":0,"title":"Solway Lass"},{"ns":0,"title":"Szymon Lass"},{"ns":0,"title":"The Bonnie Lass o' Fyvie"},{"ns":0,"title":"The Tullaghmurray Lass"},{"ns":0,"title":"Woman"},{"ns":12,"title":"Help:Disambiguation"}]}}}}
,我想將其解析到標題和在這樣一個數據類型的鏈接列表。
data WikiPage = WikiPage { title :: String,
links :: String }
什麼代碼我現在有是這樣的,
instance FromJSON WikiPage where
parseJSON j = do
o <- parseJSON j
let id = head $ o .: "query" .: "pages"
let name = o .: "query" .: "pages" .: id .: "title"
let links = mapM (.: "title") (o .: "query".: "pages" .: id .: "links")
return $ WikiPage name links
,我發現了錯誤,
Couldn't match expected type `Data.Text.Internal.Text'
with actual type `[Char]'
In the second argument of `(.:)', namely `"title"'
我真的不明白怎麼回事,我覺得好像有必須是我如何映射鏈接的問題,但我不確定究竟要做什麼。我也沒有得到如何在第二個查詢字符串中使用id,因爲它是一個解析器(我確定我需要在這裏使用application,但我不知道如何。)我還沒有找到任何分解更復雜的jsons的例子都是這樣的。
除了''OverloadedStrings''問題之外,那些'let'看起來不正確,例如,你應該用'name <-'來代替'let name =',因爲'name'目前的類型是'Parser a',但是你想''' – hvr