我很难尝试解析类似的结构
{"tree": [5, [[1, 4], [2, 3]]]}
变成二叉树
data Tree a = Leaf a | Node (Tree a) (Tree a)
但我什至似乎无法正确输入类型。这个问题有简单的解决方案吗?
我假设每个 JSON 数组正好包含两个元素,所以结果应该是
Node (Leaf 5) (Node (Node (Leaf 1) (Leaf 4)) (Node (Leaf 2) (Leaf 3))) :: Tree Int
编辑:我尝试过的:
我尝试添加用于导入的新数据类型
data IntTree = IntTree { jsonTree :: Tree Int }
instance FromJSON IntTree
where
parseJSON (Object v) = do
inttree <- (v .: "tree")
-- now I am stuck