0
我無法解析以下JSON結構中的標記。解析器只適用於當我聲明它是tags :: !Array
它失敗時,我宣佈它爲tags :: [Tag]
Haskell Aeson嵌套數組JSON
爲什麼?
{
"response": {
"status": "ok",
"results": [
{
"type": "article",
"fields": {
"wordcount": "497"
},
"tags": [
{
"id": "profile/barryglendenning"
}
]
}
]
}
}
data Field = Field{
wordcount :: Int
} deriving (Show)
instance FromJSON Field where
parseJSON (Object o) = Field <$> (o .: "wordcount")
parseJSON _ = mzero
data Tag = Tag{
id :: Text
} deriving (Show)
instance FromJSON Tag where
parseJSON (Object o) = Tag <$> (o .: "id")
parseJSON _ = mzero
data SearchResult = SearchResult {
type:: Text,
field :: Field,
tags :: !Array
} deriving (Show)
instance FromJSON SearchResult where
parseJSON (Object o) = do
let t1 = o .: "type"
let t2 = o .: "fields"
let t3 = o .: "tags"
SearchResult <$> t1 <*> t2 <*> t3
parseJSON _ = mzero
data ContentrResult = ContentrResult {
results :: [SearchResult],
status :: Text
} deriving (Show)
instance FromJSON ContentrResult where
parseJSON (Object o) = do
r <- o .: "response"
ContentrResult <$> r .: "results"
<*> r .: "status"
parseJSON _ = mzero
什麼是你所得到的確切的錯誤? – user2847643
它可能與你的問題無關,但我不會指定一個記錄字段「id」,因爲那樣你就會在前奏中產生歧義。 –
順便說一句,命名一個字段'type'是一個語法錯誤 –