libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.6.9"
scala
import org.json4s._
import org.json4s.jackson.JsonMethods._
def parseJsonData(jsonString: String): Option[JValue] = {
try {
val json = parse(jsonString)
Some(json)
} catch {
case e: Exception =>
println("Error occurred while parsing JSON data: " + e.getMessage)
None
}
}
val jsonData = """{"name": "John", "age": 30}"""
val result = parseJsonData(jsonData)
result match {
case Some(json) =>
val name = (json \ "name").extract[String]
val age = (json \ "age").extract[Int]
println("Name: " + name)
println("Age: " + age)
case None =>
println("Failed to parse JSON data.")
}