我想要一些解決JSON與Aeson解碼時如何訪問特定字段的幫助。在Haskell中解析解碼的JSON
我想了解一下當我處於這種狀態時訪問字段或字段列表的最佳方式的一些提示。
例如從下面。數據包含2種產品的說明。我將如何返回2 sku
字段,即["ABCDEF","CDEFG"]
甚至如何訪問兩個產品的整個Identity
數據類型。
我的輸出是:
DECODED JSON
Just (Response {response = [Body {productId = 5555, brandId = 10, productTypeId = 1, identity = Identity {sku = "ABCDEF", ean = "1111", barcodls {taxable = False, taxCode = TaxCode {taxCodeId = 7, code = "T1"}}, variations = [Variation {optionId = 1, optionName = "option1", optionVal111221", barcode = "2443222"}, productGroupId = 17, stock = Stock {stockTracked = True, weight = Weight {magnitude = 0.0}, dimensions = Dimensption1", optionValueId = 5, optionValue = "5"},Variation {optionId = 2, optionName = "option2", optionValueId = 14, optionValue = "14"}]}]})
原來這裏是JSON
JSON
{
"response": [
{
"id": 5555,
"brandId": 10,
"productTypeId": 1,
"identity": {
"sku": "ABCDEF",
"ean": "1111",
"barcode": "2222"
},
"productGroupId": 17,
"stock": {
"stockTracked": true,
"weight": {
"magnitude": 0
},
"dimensions": {
"length": 0,
"height": 0,
"width": 0,
"volume": 0
}
},
"financialDetails": {
"taxable": false,
"taxCode": {
"id": 7,
"code": "T1"
}
},
"variations": [
{
"optionId": 1,
"optionName": "option1",
"optionValueId": 5,
"optionValue": "5"
},
{
"optionId": 2,
"optionName": "option2",
"optionValueId": 14,
"optionValue": "OS"
}
]
},
{
"id": 9999,
"brandId": 10,
"productTypeId": 1,
"identity": {
"sku": "CDEFG",
"ean": "111221",
"barcode": "2443222"
},
"productGroupId": 17,
"stock": {
"stockTracked": true,
"weight": {
"magnitude": 0
},
"dimensions": {
"length": 0,
"height": 0,
"width": 0,
"volume": 0
}
},
"financialDetails": {
"taxable": false,
"taxCode": {
"id": 7,
"code": "T1"
}
},
"variations": [
{
"optionId": 1,
"optionName": "option1",
"optionValueId": 5,
"optionValue": "5"
},
{
"optionId": 2,
"optionName": "option2",
"optionValueId": 14,
"optionValue": "14"
}
]
}
]
}
這裏是我到目前爲止的代碼:
CODE
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Control.Applicative
import qualified Data.ByteString.Lazy.Char8 as BS
jsonFile :: FilePath
jsonFile = "test.json"
getJSON :: IO BS.ByteString
getJSON = BS.readFile jsonFile
main :: IO()
main = do
input <- getJSON
let json = decode input :: Maybe Response
case json of
Nothing -> print "error parsing JSON"
Just m -> print json
data Response = Response
{ response :: [Body]
} deriving (Show)
instance FromJSON Response where
parseJSON (Object v) = Response <$> v .: "response"
parseJSON _ = mempty
data Body = Body
{ productId :: Int
, brandId :: Int
, productTypeId :: Int
, identity :: Identity
, productGroupId :: Int
, stock :: Stock
, financialDetails :: FinancialDetails
, variations :: [Variation]
} deriving (Show)
instance FromJSON Body where
parseJSON (Object v) = Body
<$> v .: "id"
<*> v .: "brandId"
<*> v .: "productTypeId"
<*> v .: "identity"
<*> v .: "productGroupId"
<*> v .: "stock"
<*> v .: "financialDetails"
<*> v .: "variations"
parseJSON _ = mempty
data Identity = Identity
{ sku :: String
, ean :: String
, barcode :: String
} deriving (Show)
instance FromJSON Identity where
parseJSON (Object v) = Identity
<$> v .: "sku"
<*> v .: "ean"
<*> v .: "barcode"
parseJSON _ = mempty
data Stock = Stock
{ stockTracked :: Bool
, weight :: Weight
, dimensions :: Dimensions
} deriving (Show)
instance FromJSON Stock where
parseJSON (Object v) = Stock
<$> v .: "stockTracked"
<*> v .: "weight"
<*> v .: "dimensions"
parseJSON _ = mempty
data Weight = Weight
{ magnitude :: Double
} deriving (Show)
instance FromJSON Weight where
parseJSON (Object v) = Weight
<$> v .: "magnitude"
parseJSON _ = mempty
data Dimensions = Dimensions
{ length :: Double
, height :: Double
, width :: Double
, volume :: Double
} deriving (Show)
instance FromJSON Dimensions where
parseJSON (Object v) = Dimensions
<$> v .: "length"
<*> v .: "height"
<*> v .: "width"
<*> v .: "volume"
parseJSON _ = mempty
data FinancialDetails = FinancialDetails
{ taxable :: Bool
, taxCode :: TaxCode
} deriving (Show)
instance FromJSON FinancialDetails where
parseJSON (Object v) = FinancialDetails
<$> v .: "taxable"
<*> v .: "taxCode"
parseJSON _ = mempty
data TaxCode = TaxCode
{ taxCodeId :: Int
, code :: String
} deriving (Show)
instance FromJSON TaxCode where
parseJSON (Object v) = TaxCode
<$> v .: "id"
<*> v .: "code"
parseJSON _ = mempty
data Variation = Variation
{ optionId :: Int
, optionName :: String
, optionValueId :: Int
, optionValue :: String
} deriving (Show)
instance FromJSON Variation where
parseJSON (Object v) = Variation
<$> v .: "optionId"
<*> v .: "optionName"
<*> v .: "optionValueId"
<*> v .: "optionValue"
parseJSON _ = mempty
這很棒,你的解釋非常清楚。再次感謝。 – matthias