首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell Aeson嵌套数组JSON

Haskell Aeson嵌套数组JSON
EN

Stack Overflow用户
提问于 2016-08-15 03:06:13
回答 1查看 446关注 0票数 0

我在解析以下JSON结构中的标记时遇到了问题。只有当我声明它为tags :: !Array时,解析器才能工作,当我声明它为tags :: [Tag]时,解析器就失败了

为什么?

代码语言:javascript
复制
{
  "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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-15 15:53:50

Nothing对于调试并不是很有用,不是吗?

我设法获得了将tags解析为[Tag]的示例JSON。我想知道您的错误是否与wordcount字段是JSON中的String而不是Number有关。

下面是一个或多或少自包含的示例,其中我将示例JSON wordcount更改为一个数字:

代码语言:javascript
复制
{-# 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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38945660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档