开发者

scala lift json: pattern match on unknown data?

开发者 https://www.devze.com 2023-03-04 22:37 出处:网络
I have some strange json that I cannot change, and I wish to parse it using the JsonParsen in lift. A typical json is like:

I have some strange json that I cannot change, and I wish to parse it using the JsonParsen in lift.

A typical json is like:

    {"name":"xxx",  "data":{
        "data_123456":{"id":"Hello"},
        "data_789901":{"id":"Hello"},
        "data_987654":{"id":"Hello"},  
}}

The issue is that the keys for the data are unknown (data_xxxxx, where the xx:s are not known).开发者_如何学运维 This is bad json, but I have to live with it.

How am I supposed to setup case-classes in scala to be able to build a proper structure when the keys here are unknown, but the structure is known?


You can use a Map, and every value can be JValue too, representing unparsed JSON. Example:

case class Id(id: String)
case class Data(name: JValue, data: Map[String, Id])

And then:

json.extract[Data]
res0: Data(JString(xxx),Map(data_123456 -> Id(Hello), data_789901 -> Id(Hello), data_987654 -> Id(Hello)))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号