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'是一个语法错误 –