Reputation: 1672
I am getting this json response, how to create Scala Case Class for the page_views ??
"page_views": {
"2015-12-30T21:30:00+05:30": 4,
"2016-01-08T15:30:00+05:30": 25,
"2016-01-13T11:30:00+05:30": 9,
"2016-01-13T12:30:00+05:30": 8,
"2016-01-14T10:30:00+05:30": 21,
"2016-01-21T12:30:00+05:30": 19,
"2016-01-21T17:30:00+05:30": 4,
"2016-01-22T17:30:00+05:30": 2,
"2016-02-02T10:30:00+05:30": 14,
"2016-02-24T12:30:00+05:30": 11,
"2016-02-26T09:30:00+05:30": 12
},
Upvotes: 1
Views: 220
Reputation: 1672
After many tries , I was able to make it work. I used a Map
to bind to the JSON fields.
case class Test(page_views: Map[String, Int])
Upvotes: 1
Reputation: 7865
you may follow up this tutorial that covers how to parse json strings to your model. It also covers some usual transformations you may need to apply to convert from the json to your case class
but your case is kinda weird, shouldn't page_views contain/be an array? how can you process the page_views json object if you don't know which fields are in it?
Upvotes: 0
Reputation: 1285
First define what case class you want. Let's say it's something like case class PageView(date:myDateType,numberViews:Long)
. Then you don't fall in the basic case where the json you receive has the fields date
and numberViews
explicitly written, e.g {"date":"xxx","numberViews":123}
. So using json4s it won't be enough to create a case class and let it do the rest, you will have to write a custom (de)serializer (they have an example here, search 'Serializer' on the page).
Upvotes: 1