解码haskell中的复杂字符串

时间:2014-11-03 20:55:16

标签: json haskell getjson

我已经制作了一个json到haskell解析器,它工作绝对正确,解析器是

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>                                              

有人对此有所了解吗?

1 个答案:

答案 0 :(得分: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