1
我正在使用提升JSON的解析來解析某些JSON。 JSON是遞歸的,例如每個級別都存在id
字段。下面是一個例子:在提升JSON中的非遞歸提取以便理解
val json = """
{
"id": 1
"children": [
{
"id": 2
},
{
"id": 3
}
]
}
"""
以下代碼
var ids = for {
JObject(parent) <- parse(json)
JField("id", JInt(id)) <- parent
} yield id
println(ids)
產生List(1, 2, 3)
。我期待它能生產List(1)
。
在我的程序中,這導致二次計算,但我只需要線性。
是否可以使用for-comprehensions來匹配頂級id
字段?