美文网首页
golang校验json数据内容

golang校验json数据内容

作者: 杜子龙 | 来源:发表于2019-08-20 16:15 被阅读0次
package main

import (
    "fmt"
    "github.com/xeipuuv/gojsonschema"
)

func main() {

    schemaLoader := gojsonschema.NewStringLoader(`{"type": "object","properties":{"a":{"type":"object"}},"required":["a"]}`) // json格式
    documentLoader := gojsonschema.NewStringLoader(`{"a":"b"}`) // 待校验的json数据

    result, err := gojsonschema.Validate(schemaLoader, documentLoader)
    if err != nil {
        panic(err.Error())
    }

    if result.Valid() {
        fmt.Printf("The document is valid\n")
    } else {
        fmt.Printf("The document is not valid. see errors :\n")
        for _, desc := range result.Errors() {
            fmt.Printf("- %s\n", desc)
        }
    }
}

参考:

  1. https://github.com/xeipuuv/gojsonschema
  2. https://www.cnblogs.com/huanghongbo/p/8628607.html
  3. https://blog.csdn.net/ccq1127/article/details/88061531

相关文章

网友评论

      本文标题:golang校验json数据内容

      本文链接:https://www.haomeiwen.com/subject/tlbpsctx.html