0

I have a json string like below, please suggest what changes should be done to my code in order to parse this json into a case class in Scala?

    {
        "boundaries": [
          [
            [
              -11110372.022426892,
              4676428.402837045
            ],
            [
              -11124418.538414171,
              4740594.245854561
            ],
            [
              -11101812.444140816,
              4744556.315065523
            ],
            [
              -11087326.99540134,
              4684866.726958438
            ],
            [
              -11108506.41908069,
              4677271.949698344
            ],
            [
              -11110152.500391051,
              4676569.01290604
            ],
            [
              -11110372.022426892,
              4676428.402837045
            ]
          ]
        ]
      }

I have tried the below code, but it doesnt work


import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.JsonAST._
import org.json4s.jackson.JsonMethods._

val jsonString = """<the string given above>"""
val jsonAst: JValue = parse(jsonString)

case class Boundaries(r : Array[Array[Array[Array[Double]]]])

How to associate this case class to the json string and get all the numerical values in this class?

0

1 Answer 1

2

This worked for me:

import org.json4s.jackson.JsonMethods._
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.JsonAST._
import org.json4s.jackson.JsonMethods._

implicit val formats = DefaultFormats

case class Boundaries (boundaries: List[List[List[Double]]])

val jsonString = """
 <your json>
"""

scala> val jsonAst: JValue = parse(jsonString)
jsonAst: org.json4s.JsonAST.JValue = <parsed JValue>

scala> jsonAst.extract[Boundaries]
res0: Boundaries = Boundaries(List(List(List(-1.1110372022426892E7, 4676428.402837045), List(-1.1124418538414171E7, 4740594.245854561), List(-1.1101812444140816E7, 4744556.315065523), List(-1.108732699540134E7, 4684866.726958438), List(-1.110850641908069E7, 4677271.949698344), List(-1.1110152500391051E7, 4676569.01290604), List(-1.1110372022426892E7, 4676428.402837045))))
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please advise how to extract all the numerical values after the last step? Say for example, I do val b = (jsonAst).extract[Boundaries] and then how to extract numerical values using b?
Just like you do it with any list of elements? Also it depends on what you ultimately intend to do with this object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.