Parsing a dynamic value with Lift-JSON

Posted by Surya Suravarapu on Stack Overflow See other posts from Stack Overflow or by Surya Suravarapu
Published on 2010-05-22T15:08:17Z Indexed on 2010/05/22 15:10 UTC
Read the original article Hit count: 268

Filed under:
|
|

Let me explain this question with an example. If I have a JSON like the following:

{"person1":{"name": "Name One", "address": {"street": "Some Street","city": "Some City"}},
"person2":{"name": "Name Two", "address": {"street": "Some Other Street","city": "Some Other City"}}}

[There is no restriction on the number of persons, the input JSON can have many more persons]

I could extract this JSON to Persons object by doing

var persons = parse(res).extract[T]

Here are the related case classes:

case class Address(street: String, city: String)
case class Person(name: String, address: Address, children: List[Child])
case class Persons(person1: Person, person2: Person)

Question: The above scenario works perfectly fine. However the need is that the keys are dynamic in the key/value pairs. So in the example JSON provided, person1 and person2 could be anything, I need to read them dynamically. What's the best possible structure for Persons class to account for that dynamic nature.

© Stack Overflow or respective owner

Related posts about JSON

Related posts about scala