I try (and cannot) figure out how spray json turns json into objects. If I have a simple key -> json feed value, then it works fine, but the data I want to read is on a list like this:
[{ "name": "John", "age": "30" }, { "name": "Tom", "age": "25" }]
And my code looks like this:
package jsontest import spray.json._ import DefaultJsonProtocol._ object JsonFun { case class Person(name: String, age: String) case class FriendList(items: List[Person]) object FriendsProtocol extends DefaultJsonProtocol { implicit val personFormat = jsonFormat2(Person) implicit val friendListFormat = jsonFormat1(FriendList) } def main(args: Array[String]): Unit = { import FriendsProtocol._ val input = scala.io.Source.fromFile("test.json")("UTF-8").mkString.parseJson val friendList = input.convertTo[FriendList] println(friendList) } }
If I modify my test file so that it just has one person not in the array and run val friendList = input.convertTo[Person] , then it will work and everything will be analyzed, but as soon as I try and analyze the array, it suffers failed with error Object expected in field 'items'
Can someone point me in the direction of what I am doing wrong?
source share