将具有多个元组的JSON解析为List< object>在斯卡拉

问题描述:

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

我有这种格式的JSON字符串, 现在我需要转换每个元组-> {"fname":"Foo","lname":"Pacman"}

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

对于一个Person对象, 例如假设我有一个案例课

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

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

现在我要如何获得,List<person>

如果我有一个包含用于单个元组的数据的JSON,那么我可以,

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.

Jerkson支持开箱即用地反序列化对象列表,因此您所需要做的就是:

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

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