简体   繁体   English

在Scala播放中遍历嵌套的json

[英]Traversing through nested json in scala play

I'm using scala play and am attempting to traverse a json tree in order to validate that specific name values have specific children with specific name values. 我正在使用scala播放,并尝试遍历json树以验证特定名称值具有具有特定名称值的特定子代。 I have the following Json in the form of a JsObject: 我有以下Json以JsObject的形式:

{ "name": "user", "children": [ { "name": "$a", "children": [ { "name": "foo", "children": [  ] }, { "name": "fooBar", "children": [ { "name": "$a", "children": [ { "name": "subFoobar1", "children": [  ] }, { "name": "subFoobar2", "children": [ { "name": "TEST", "children": [  ] } ] }, { "name": "subFoobar3", "children": [  ] } ] } ] }, { "name": "bar", "children": [ { "name": "$a", "children": [  ] }, { "name": "$c", "children": [  ] }, { "name": "$b", "children": [  ] } ] }, { "name": "barFoo", "children": [  ] } ] } ] }

Ideally I would use nested for loops to traverse but the JsObject structure is preventing me from accessing the underlying values when attempting traverse. 理想情况下,我将使用嵌套的for循环遍历,但是JsObject结构阻止了我在尝试遍历时访问基础值。 I have also attempted mapping the JsObject to a map of type [Map[String,Map[String,Any]]] but I am getting invalid cast compiler errors. 我也尝试将JsObject映射到[Map [String,Map [String,Any]]]类型的映射,但是我得到了无效的强制转换编译器错误。

Any tips on how I can traverse and validate the name value at each level would be appreciated. 关于如何在每个级别遍历和验证名称值的任何提示将不胜感激。 I would preferably like to use the play json library 我最好使用play json库

Issue was in the case class I was attempting to use. 问题出在我尝试使用的案例类中。 I wasn't accounting for the recursive nature of my Json structure 我没有考虑我的Json结构的递归性质

case class ActorTree(name : String, children:Seq[ActorTree] )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM