当前位置:网站首页 > Java教程 > 正文

java json schema教程



 @PostMapping("/test") @ResponseBody public String test() { InputStream test = getClass().getResourceAsStream("/static/json/test.json"); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "a"); map1.put("age", 10); JSONObject json1 = new JSONObject(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", 1); map2.put("age", "12"); JSONObject json2 = new JSONObject(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("name", "a"); map3.put("sex", "0"); JSONObject json3 = new JSONObject(map3); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("name", "a"); map4.put("age", 11); map4.put("sex", 0); JSONObject json4 = new JSONObject(map4); Map<String, Object> map5 = new HashMap<String, Object>(); map5.put("name", "ad"); map5.put("age", "12"); map4.put("sex", 0); JSONObject json5 = new JSONObject(map5); JSONObject Schema = new JSONObject(new JSONTokener(test)); Schema schema = SchemaLoader.load(Schema); System.out.println("—————————data1———————————"); try { schema.validate(json1); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data2———————————"); try { schema.validate(json2); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data3———————————"); try { schema.validate(json3); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data4———————————"); try { schema.validate(json4); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data5———————————"); try { schema.validate(json5); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } return "hello"; }

版权声明


相关文章:

  • java分层架构教程2025-01-10 14:58:06
  • 大一java教程2025-01-10 14:58:06
  • 低频红石教程java2025-01-10 14:58:06
  • java渗透 教程2025-01-10 14:58:06
  • java教程42025-01-10 14:58:06
  • Java redis实战教程2025-01-10 14:58:06
  • java异步文件教程2025-01-10 14:58:06
  • 算法很美java教程99讲2025-01-10 14:58:06
  • java教程dvd光盘2025-01-10 14:58:06
  • java高级教程16节2025-01-10 14:58:06