2014-11-03 147 views
-2

一個複雜的字符串我已經做了JSON哈斯克爾解析器這是工作完全正確的解析器解碼哈斯克爾

decodeToMaybeValue::BLC.ByteString->Maybe Value       
decodeToMaybeValue = decode 

main = do            
    interact (show . decodeToMaybeValue . BLC.pack)           

該編譯器直接編譯時工作絕對正確的,但是當我嘗試存儲將字符串放入一個變量來解碼它會產生這個錯誤。我試圖

x =`"{\"apiVersion\": \"2.0\",\"data\": {\"updated\": \"2010-01-07T19:58:42.949Z\",\"totalItems\": 800,\"startIndex\": 1,\"itemsPerPage\": 1,\"items\": [{\"id\": \"hYB0mn5zh2c\",\"uploaded\":\"2007-06-05T22:07:03.000Z\",\"updated\": \"2010-01-07T13:26:50.000Z\",\"uploader\": \"GoogleDeveloperDay\",\"category\": \"News\",\"title\": \"Google Developers Day US - Maps API Introduction\",\"description\": \"Google Maps API Introduction ...\",\"tags\": [\"GDD07\",\"GDD07US\",\"Maps\"],\"duration\": 2840,\"aspectRatio\": \"widescreen\",\"rating\": 4.63,\"ratingCount\": 68,\"viewCount\": 220101,\"favoriteCount\":201,\"commentCount\": 22 }]}}"`    
y = BLC.pack x                         

Invalid type signature: decode y :: Maybe Value 
-- Should be of form <variable> :: <type>            

有沒有人有想法呢?

回答

0

你將不再需要你的字符串轉換,如果你在該文件的第一行使用{-# LANGUAGE OverloadedStrings #-}爲文本....

這是你想要的嗎?

{-# LANGUAGE OverloadedStrings #-} 

import Data.Aeson 
import qualified Data.ByteString.Lazy.Char8 as BLC 

decodeToMaybeValue::BLC.ByteString->Maybe Value       
decodeToMaybeValue = decode 

theData="{\"a\": \"b\"}" --Put in the data here. 

main = do            
    print $ decodeToMaybeValue theData 
+0

是的,我完全想要這個 – 2014-11-03 21:27:00