76

如果在使用 Go 解析 JSON 输入时未找到字段,是否可能生成错误?

我在文档中找不到它。

是否有任何标签根据需要指定字段?

4

5 回答 5

74

encoding/json包中没有将字段设置为“必需”的标签。您要么必须编写自己的MarshalJSON()方法,要么对缺失的字段进行后期检查。

要检查缺失的字段,您必须使用指针来区分缺失/空值和零值:

type JsonStruct struct {
    String *string
    Number *float64
}

完整的工作示例:

package main

import (
    "fmt"
    "encoding/json"
)

type JsonStruct struct {
    String *string
    Number *float64
}

var rawJson = []byte(`{
    "string":"We do not provide a number"
}`)


func main() {
    var s *JsonStruct
    err := json.Unmarshal(rawJson, &s)
    if err != nil {
        panic(err)
    }

    if s.String == nil {
        panic("String is missing or null!")
    }

    if s.Number == nil {
        panic("Number is missing or null!")
    }

    fmt.Printf("String: %s  Number: %f\n", *s.String, *s.Number)
}

操场

于 2013-10-28T11:53:28.437 回答
20

您还可以覆盖特定类型的解组(因此需要的字段隐藏在几个 json 层中),而不必使该字段成为指针。UnmarshalJSON 由Unmarshaler接口定义。

type EnumItem struct {                                                                                            
    Named                                                                                                         
    Value string                                                                                                  
}                                                                                                                 

func (item *EnumItem) UnmarshalJSON(data []byte) (err error) {                                                    
    required := struct {                                                                                          
        Value *string `json:"value"`                                                                              
    }{}                                                                                                           
    all := struct {                                                                                               
        Named                                                                                                     
        Value string `json:"value"`                                                                               
    }{}                                                                                                           
    err = json.Unmarshal(data, &required)                                                                         
    if err != nil {                                                                                               
        return                                                                                                    
    } else if required.Value == nil {                                                                             
        err = fmt.Errorf("Required field for EnumItem missing")                                                   
    } else {                                                                                                      
        err = json.Unmarshal(data, &all)                                                                          
        item.Named = all.Named                                                                                    
        item.Value = all.Value                                                                                    
    }                                                                                                             
    return                                                                                                        
}                                                       
于 2016-08-24T22:41:20.333 回答
5

这是通过检查您的定制的另一种方式tag

您可以为您的结构创建一个标签,例如:

type Profile struct {
    Name string `yourprojectname:"required"`
    Age  int
}

用于reflect检查标签是否被required赋值

func (p *Profile) Unmarshal(data []byte) error {
    err := json.Unmarshal(data, p)
    if err != nil {
        return err
    }

    fields := reflect.ValueOf(p).Elem()
    for i := 0; i < fields.NumField(); i++ {

        yourpojectTags := fields.Type().Field(i).Tag.Get("yourprojectname")
        if strings.Contains(yourpojectTags, "required") && fields.Field(i).IsZero() {
            return errors.New("required field is missing")
        }

    }
    return nil
}

测试用例如下:

func main() {

    profile1 := `{"Name":"foo", "Age":20}`
    profile2 := `{"Name":"", "Age":21}`

    var profile Profile

    err := profile.Unmarshal([]byte(profile1))
    if err != nil {
        log.Printf("profile1 unmarshal error: %s\n", err.Error())
        return
    }
    fmt.Printf("profile1 unmarshal: %v\n", profile)

    err = profile.Unmarshal([]byte(profile2))
    if err != nil {
        log.Printf("profile2 unmarshal error: %s\n", err.Error())
        return
    }
    fmt.Printf("profile2 unmarshal: %v\n", profile)

}

结果:

profile1 unmarshal: {foo 20}

2009/11/10 23:00:00 profile2 unmarshal error: required field is missing

你可以去Playground看看完成的代码

于 2020-09-21T23:06:56.507 回答
3

你可以只实现Unmarshaler接口来自定义你的 JSON 如何被解组。

于 2019-12-03T07:45:41.773 回答
0

您还可以使用 JSON 模式验证。

package main

import (
    "encoding/json"
    "fmt"

    "github.com/alecthomas/jsonschema"
    "github.com/xeipuuv/gojsonschema"
)

type Bird struct {
    Species     string `json:"birdType"`
    Description string `json:"what it does" jsonschema:"required"`
}

func main() {
    var bird Bird
    sc := jsonschema.Reflect(&bird)
    b, _ := json.Marshal(sc)

    fmt.Println(string(b))

    loader := gojsonschema.NewStringLoader(string(b))
    documentLoader := gojsonschema.NewStringLoader(`{"birdType": "pigeon"}`)

    schema, err := gojsonschema.NewSchema(loader)
    if err != nil {
        panic("nop")
    }
    result, err := schema.Validate(documentLoader)
    if err != nil {
        panic("nop")
    }

    if result.Valid() {
        fmt.Printf("The document is valid\n")
    } else {
        fmt.Printf("The document is not valid. see errors :\n")
        for _, err := range result.Errors() {
            // Err implements the ResultError interface
            fmt.Printf("- %s\n", err)
        }
    }

}

输出

{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/Bird","definitions":{"Bird":{"required":["birdType","what it does"],"properties":{"birdType":{"type":"string"},"what it does":{"type":"string"}},"additionalProperties":false,"type":"object"}}}
The document is not valid. see errors :
- (root): what it does is required

取自Strict JSON parsing的代码示例

于 2021-08-26T09:00:20.350 回答