[英]Parse Json Rose Tree with Haskell Aeson
我正在嘗試解析遞歸JSON數據,如下所示:
{
"node": "a",
"children": [
{
"node": "b",
"children": [
{
"node": "c",
"children": null
}
]
},
{
"node": "d",
"children": null
}
]
}
現在,我想實現FromJSON的一個實例,以便可以將其解碼為這樣的數據結構:以下數據結構
data Tree = Node { value :: Text, children :: [Tree]} | Nothing
我不知道該怎么做。 我僅看到了有關如何使用Aeson派生平面(非遞歸)JSON結構實例的示例。
正如Willem von Onsem所建議的那樣,如果您這樣定義數據,則這要簡單得多:
data Tree = Node { value :: Text, children :: [Tree]}
,僅使用一個空列表來表示(也在json中)空子樹。 然后,你就直接派生只是解析JSON fromJSON
的數據類型。
另一個想法是使用Data.Tree
已經有一個派生的實例fromJSON
。 對於Data.Tree,JSON必須像這樣構造:
["a",["b",[]]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.