JSON 模式(Schema)
来源:互联网 发布:淘宝花钱推广有用吗 编辑:程序博客网 时间:2024/04/29 18:59
JSON 模式是一种基于 JSON 格式定义 JSON 数据结构的规范。
现在,RESTful接口和JSON大行其道,我们可以把JSON直接存储到数据库中,提供了比原先的关系表更容易扩展的能力(虽然JSON串的存储仍然用到了关系表)。RESTful接口直接返回JSON数据,比返回xml更加简洁和易读。xml中可以有xml schema对xml数据进行定义和校验,同样的,JSON中也有相应的叫做JSON schema的机制,来对JSON数据进行描述和定义,并且提供了相应的机制来检验某个JSON字符串是否符合JSON schema的定义。
我们来看一下可以用于这一模式中的各种重要关键字:
测试json
[ { "id": 2, "name": "An ice sculpture", "price": 12.50 }, { "id": 3, "name": "A blue mouse", "price": 25.50 }]
json模式示例
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "Product", "type": "array", "items": { "type": "object", "properties": { "id": { "description": "The unique identifier for a product", "type": "integer" }, "name": { "description": "Name of the product", "type": "string" }, "price": { "type": "number", "minimum": 0, "exclusiveMinimum": true } }, "required": ["id", "name", "price"] }}
要检验一个给定的JSON字符串是否符合一个给定的JSON schema,在java中我们可以使用 json-schame-validator https://github.com/fge/json-schema-validator
下边是JSON schema 校验的相关代码:
import java.io.IOException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.fasterxml.jackson.databind.JsonNode;import com.github.fge.jackson.JsonLoader;import com.github.fge.jsonschema.core.exceptions.ProcessingException;import com.github.fge.jsonschema.core.report.ProcessingReport;import com.github.fge.jsonschema.main.JsonSchema;import com.github.fge.jsonschema.main.JsonSchemaFactory;public class JsonShemaValidator { private static Log log = LogFactory.getLog(JsonShemaValidator.class); private final static JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); /** * validate instance and Schema,here including two functions. as follows: * first: the Draft v4 will check the syntax both of schema and instance. * second: instance validation. * * @param mainSchema * @param instance * @return * @throws IOException * @throws ProcessingException */ public static ProcessingReport validatorSchema(String mainSchema, String instance) throws IOException, ProcessingException { JsonNode mainNode = JsonLoader.fromString(mainSchema); JsonNode instanceNode = JsonLoader.fromString(instance); JsonSchema schema = factory.getJsonSchema(mainNode); ProcessingReport processingReport = schema.validate(instanceNode); log.info(processingReport); return processingReport; }}
从返回的ProcessingReport对象中可以得到校验的结果:成功与否,以及出错信息等等
附:
1、这是自动生成json-schema的工具,可以根据json字符串生成对应的json-schema内容,功能很强大.
http://jsonschema.net/#/home
2、这个网址是通过json-schema来校验json字符串是否能够通过验证,上面的文本域用于输入json-schema内容,下面的文本域用于输入json内容,验证不通过会返回相应验证未通过的地方,验证通过返回success.
http://json-schema-validator.herokuapp.com/
3、最后一个是校验json字符串的格式是否正确
http://www.bejson.com/
本博客参考文章:
http://www.cnblogs.com/jiaoyiping/p/4799754.html
http://blog.csdn.net/a_finder/article/details/46746559
http://wiki.jikexueyuan.com/project/json/schema.html
- JSON 模式(Schema)
- JSON- 框架模式(Schema)
- Schema(模式)
- Json schema
- json schema
- json schema
- JSON Schema
- Json Schema
- JSON Schema(官方)
- JSON-Schema学习心得
- Json schema规范写法
- JSON Schema Validator
- JSON Schema详解
- JSON schema简介
- PostgreSQL学习手册(模式schema)
- Solr的Schema API(模式)
- JSON_模式(Schema)与数据解析
- 设置默认模式schema
- javascript 一维数组和二维数组
- 【pta】实验1-1 Hello World!
- Markdown语法
- 我们变成了最小的,当我们发现不了最弱小的时候
- 解决Unable to load native-hadoop library for your platform
- JSON 模式(Schema)
- SVN使用指引(本地服务器为Windows)
- hdu 1695 hdu 2841 容斥原理
- C++ CCF 出现次数最多的数
- request.getParameterValues 与 request.getParameter的区别
- 第三十一讲项目1.2-求1到m的平方和
- Android广播BroadcastReceiver最简单使用示例
- LeetCode学习篇二十三——Path Sum
- 二维码制作带图片(图片包裹二维码)