scala
import org.json.JSONArray
import org.json.JSONObject
scala
val jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
val jsonObject = new JSONObject(jsonString)
val age = jsonObject.getInt("age")
val city = jsonObject.getString("city")
scala
val jsonObject = new JSONObject()
jsonObject.put("name", "John")
jsonObject.put("age", 30)
jsonObject.put("city", "New York")
val jsonString = jsonObject.toString()
scala
import org.json.JSONArray
import org.json.JSONObject
object ScalaJSONExample {
def main(args: Array[String]): Unit = {
val jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
val jsonObject = new JSONObject(jsonString)
val name = jsonObject.getString("name")
val age = jsonObject.getInt("age")
val city = jsonObject.getString("city")
println(s"Name: $name")
println(s"Age: $age")
println(s"City: $city")
val jsonObject = new JSONObject()
jsonObject.put("name", "John")
jsonObject.put("age", 30)
jsonObject.put("city", "New York")
val jsonString = jsonObject.toString()
println(jsonString)
}
}