2
我想解析使用埃宋以下JSON在Haskell:埃宋:未知重點解析JSON在Haskell
{
"foo": {
"name": "name 1",
"location": "location 1"
},
"bar": {
"name": "name 2",
"location": "location 2"
}
}
name
和location
已知的關鍵,但foo
和bar
是未知的。
我想加載JSON數據([Entry]
)以下的數據類型的列表:
data Entry = Entry
{ id :: String -- "foo" or "bar" etc.
, name :: String -- "name 1" or "name 2" etc.
, location :: String -- "location 1" or "location 2" etc.
} deriving Show
我第一次嘗試如下所示(它不工作,還):
instance FromJSON Entry where
parseJSON (Object o) = do
map (\(id, (name, location)) -> Entry id name location) o
parseJSON o
我將如何正確解析JSON?
非常感謝,它完美的作品。什麼是字符串「入門體」? – watain
@watain如果應用給定函數應用於錯誤類型的值,那麼傳遞給'typeMismatch'的參數就是這樣。 – chepner