如果你不已經與通用這樣做,這裏是用TEXT.JSON
的實例的方式
import Text.JSON
data SO = SO {
name :: String,
mytype :: Int,
code :: String,
options :: [Option]
} deriving (Show)
data Option =Option {
atb :: KV
}
data KV = KV {
desc :: String,
v:: Bool
}
instance JSON SO where
showJSON ge = makeObj
[ ("name", showJSON $ name ge),
("type", showJSON $ mytype ge),
("options", showJSON $ options ge)
]
readJSON = error "readJSON not implemented for SO"
instance JSON Option where
showJSON ge = makeObj
[ ("atb", showJSON $ atb ge)
]
readJSON = error "readJSON not implemented for Option"
instance JSON KV where
showJSON ge = makeObj
[ ("description", showJSON $ desc ge),
[ ("value", showJSON $ v ge)
]
readJSON = error "readJSON not implemented for kv"
--encode $ SO .........
每個鍵 - 值對的值可以是數組還是對象? –
我無法想象它不可能。我的懷疑是你只需要添加一個以'atb'作爲鍵和另一個JSObject作爲值的kv對。 – evanmcdonnal