开发者

scala lift json: pattern match on unknown data?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜