json schema校正

來源:互聯網
上載者:User

標籤:init   bsp   ice   iss   exclusive   nim   sina   strong   字元   

工作中使用到了json schema格式校正的問題,在網上查了些資料,結合自己的理解記錄一下。

json schema可以對json結果字串做出一些約束,例如:

1. 實值型別是:array, object, number, string等等

2.實值型別必須是枚舉中的一個

3. 字串的長度限制

4. 對字串判斷是否符合Regex

5. array元素個數

6. object對象必要屬性

 

測試schema檔案

 1 { 2     "$schema": "http://json-schema.org/draft-04/schema#", 3     "title": "Product set", 4     "type": "array", 5     "items": { 6         "title": "Product", 7         "type": "object", 8         "properties": { 9             "name": {10                 "type": "string"11             },12             "price": {13                 "type": "number",14                 "minimum": 0,15                 "maximum":30,16                 "exclusiveMinimum": true17             },18             "tags": {19                 "type": "array",20                 "items": {21                     "type": "string",22                     "minLength":10,23                     "maxLength":1024,24                     "pattern":"http://.*"25                 },26                 "minItems": 2,27                 "uniqueItems": true28             },29             "city":{30               "type":"string",31               "enum" : ["bj", "sh", "hk"]32             }33         },34         "required": ["name", "price"]35     }36 }

 

測試json

 1 [ 2   { 3     "name":"san", 4     "price":29, 5     "tags":["http://sdtr/sdg", "http://qwewret/qsdf"], 6     "city":"bj" 7   }, 8   { 9     "name":"dong",10     "price":30,11     "tags":["http://test", "http://sina"],12     "city":"hk"13   },14 ]

 

參考:

http://blog.csdn.net/Miss_Ashelley/article/details/53285762

http://www.jsonschemavalidator.net/

http://json-schema.org/

 

json schema校正

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.