Welcome,
I really wanted to avoid this but i give up for now. I am trying to traverse through json array and withdraw some elements and put them to sequence. I tried many options, tried all from play docs, many attempts and still i have the same error: null pointer exception when my method try to withdraw elements. So my json example is:
[
{
"var1": "xx",
"var2": "xxx",
"var3": 111
},
{
"var1": "yy",
"var2": "yyy",
"var3": 222
},
{
"var1": "zz",
"var2": "zzz",
"var3": 333
}
]
I defined many configurations of Reads, last one is like that but doesnt want to compile even ;/
case class Vars1(vars: Seq[String])
val var1Reads: Reads[String] = (__ \ "var1").read[String]
implicit val vars1Reads: Reads[Vars1] = ((__).read[Seq[String]])(Vars1.apply _)
I want to create Sequence or List, this not important, from only var1 from each element of this array. It can be pure sequence, or case class with sequence or other. Just how to iterate through json arrays? I tried many configurations, reading var1 is nonproblematic, but when i want read all vars 1 from each element of array then i have null pointer exception. Please, anyone can point me in a good direction, give me a hint what i am doing wrong? Please don't blame I am newbie at programming, and i started to learn scala and play framework.
Thank you for any help.
Update:
I have tried this one too:
implicit val vars1Reads = (__).read(Reads.list((__).read[String])).map( var => Vars(var))
but still i have the same error:
[NoSuchElementException: JsError.get]
implicit lazy val vars1Reads
. That's an annoying and altogether confusing source of NPEs. – Ryan