2016-08-14 5 views
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 
+2

당신이 점점 정확한 오류가 무엇입니까 : 여기

내가 숫자로 예를 JSON의 단어 수를 변경 더 - 또는 - 덜 자기 - 포함 된 예입니다? – user2847643

+0

아마 문제와 관련이 없지만 레코드 필드의 이름을'id'로 지정하지 않을 것입니다. 왜냐하면 서곡과 모호함을 갖기 때문입니다. –

+0

그건 그렇고, 필드 형식을'타입'은 구문 오류입니다. –

답변

1

Nothing은 디버깅에 유용하지 않습니까?

tags을 구문 분석하여 JSON 예제를 [Tag]으로 가져올 수있었습니다. 귀하의 오류가 wordcount 필드와 관련되어 있을지 궁금합니다 (Number이 아닌 JSON의 String 필드).

{-# 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)