Search code examples
jsonscalajerkson

Parsing JSON with multiple tuples to List<object> in scala


[
{"fname":"Foo","lname":"Pacman"},
{"fname":"Bar","lname":"Mario"},
{"fname":"Poo","lname":"Wario"}
]

Well I have JSON string in this format, Now what I need is to convert each tuples -> {"fname":"Foo","lname":"Pacman"}

To a Person object, for e.g. lets assume I have a case class

case class Person(fname:String,lname:String)

Now how am I to get, List<person>

If I had a JSON containing data for single tuple, then I could,

val o:Person = parse[Person](jsonString)// I am actually using Jerkson Lib

But since there are more than one tuples, how am i to parse them individually and create objects and create a list.


Solution

  • Jerkson supports deserializing lists of objects out of the box, so all you should need to do is:

    val people = parse[List[Person]](personJson)