Haskell Aeson 嵌套数组JSON
Haskell Aeson nested array JSON
我无法解析以下 JSON 结构中的标签。解析器仅在我将其声明为 tags :: !Array
时才工作 当我将其声明为 tags :: [Tag]
时它失败
为什么?
{
"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
Nothing
对调试不是很有用吗?
我设法让你的示例 JSON 将 tags
解析为 [Tag]
。我想知道您的错误是否与 wordcount
字段是 JSON 中的 String
而不是 Number
.
有关
这是一个或多或少的独立示例,其中我将示例 JSON wordcount 更改为数字:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Lib (str)
import Control.Monad (mzero)
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as LBSC
import Data.Text
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 {
typ :: Text,
fields :: Field,
tags :: [Tag]
} deriving (Show)
instance FromJSON SearchResult where
parseJSON (Object v) = SearchResult <$> v .: "type" <*> v .: "fields" <*> v .: "tags"
parseJSON _ = mzero
data ContentrResult = ContentrResult {
results :: [SearchResult],
status :: Text
} deriving (Show)
instance FromJSON ContentrResult where
parseJSON (Object v) = ContentrResult <$> v.: "results" <*> v .: "status"
parseJSON _ = mzero
data Response = Response {
response :: ContentrResult
} deriving (Show)
instance FromJSON Response where
parseJSON (Object v) = Response <$> v .: "response"
parseJSON _ = mzero
responseJson :: String
responseJson = [str|
{
"response": {
"status": "ok",
"results": [
{
"type": "article",
"fields": {
"wordcount": 497
},
"tags": [
{
"id": "profile/barryglendenning"
}
]
}
]
}
}
|]
main :: IO ()
main = do
print r
putStrLn ""
where
r :: Maybe Response
r = decode (LBSC.pack responseJson)
我无法解析以下 JSON 结构中的标签。解析器仅在我将其声明为 tags :: !Array
时才工作 当我将其声明为 tags :: [Tag]
为什么?
{
"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
Nothing
对调试不是很有用吗?
我设法让你的示例 JSON 将 tags
解析为 [Tag]
。我想知道您的错误是否与 wordcount
字段是 JSON 中的 String
而不是 Number
.
这是一个或多或少的独立示例,其中我将示例 JSON wordcount 更改为数字:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Lib (str)
import Control.Monad (mzero)
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as LBSC
import Data.Text
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 {
typ :: Text,
fields :: Field,
tags :: [Tag]
} deriving (Show)
instance FromJSON SearchResult where
parseJSON (Object v) = SearchResult <$> v .: "type" <*> v .: "fields" <*> v .: "tags"
parseJSON _ = mzero
data ContentrResult = ContentrResult {
results :: [SearchResult],
status :: Text
} deriving (Show)
instance FromJSON ContentrResult where
parseJSON (Object v) = ContentrResult <$> v.: "results" <*> v .: "status"
parseJSON _ = mzero
data Response = Response {
response :: ContentrResult
} deriving (Show)
instance FromJSON Response where
parseJSON (Object v) = Response <$> v .: "response"
parseJSON _ = mzero
responseJson :: String
responseJson = [str|
{
"response": {
"status": "ok",
"results": [
{
"type": "article",
"fields": {
"wordcount": 497
},
"tags": [
{
"id": "profile/barryglendenning"
}
]
}
]
}
}
|]
main :: IO ()
main = do
print r
putStrLn ""
where
r :: Maybe Response
r = decode (LBSC.pack responseJson)