![](/img/trans.png)
[英]how can to parse in c# JSON with dynamic key using JSON.NET or any other package
[英]Can parse JSON using the json package in GHCI but not when compiled with GHC
我正在嘗試使用json庫解析JSON字符串。 此代碼在GHCI中完美運行:
import Text.JSON as JS
JS.decode "{}" :: Result JSValue
但是當我在編譯的程序中使用相同的代碼時:
case JS.decode "{}" of
JS.Ok value -> putStrLn value
JS.Error err -> error err
它無法解析String:
Unable to read String
我懷疑這只是我做了一些愚蠢的事情,但我無法弄清楚是什么......
任何想法都非常歡迎!
更新:
我編寫了這段代碼,看看它是否是應用程序其他部分引起問題的原因:
import qualified Text.JSON as JS
main :: IO ()
main = do
case JS.decode "{}" of
JS.Ok value -> putStrLn value
JS.Error err -> error err
但是我得到了同樣的錯誤:
test: Unable to read String
它是用GHC 7.0.3編譯的,源代碼是用vim編輯的。 但是,即使從應用程序外部傳入的字符串數據也會產生相同的錯誤。 現在真的沒有想法......
使用print
代替putStrLn
,它只適用於字符串:
import qualified Text.JSON as JS
main :: IO ()
main = do
case JS.decode "{}" of
JS.Ok value -> print (value :: JSValue)
JS.Error err -> error err
我認為這只是JS.Decode
而不是JS.decode
的大寫問題。 使用最新的Haskell平台,以下工作正常。
module Foo where
import qualified Text.JSON as JS
foo :: IO ()
foo = case JS.decode "{}" of
JS.Ok value -> putStrLn value
JS.Error err -> error err
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.