dependencies { implementation 'io.spray:spray-json_2.13:1.3.6' } import spray.json.*; public class JsonExample { public static void main(String[] args) { JsValue json = JsObject.apply( "name", JsString.apply("John"), "age", JsNumber.apply(25), "isStudent", JsBoolean.apply(true) ); System.out.println(json.toString()); } } import spray.json.*; public class JsonExample { public static void main(String[] args) { String jsonString = "{\"name\":\"John\",\"age\":25,\"isStudent\":true}"; JsValue json = JsonParser.apply(jsonString); String name = json.asJsObject().get("name").get().toString(); int age = json.asJsObject().get("age").flatMap(JsValue::asNumber).map(Number::intValue).get(); boolean isStudent = json.asJsObject().get("isStudent").flatMap(JsValue::asBoolean).get(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Is Student: " + isStudent); } }


上一篇:
下一篇:
切换中文