如果json字节字段与特定类型匹配,我如何仅将其解组到字段中?

如果json字节字段与特定类型匹配,我如何仅将其解组到字段中?,json,go,encoding,types,Json,Go,Encoding,Types,我想做的是评论: type foo struct { Message string `json:"message"` } func bar() { //The "message" field contains a bool type which does not //match the equivalent "message" field in foo, a string type jsonData := byte[]("{ \"message\": true }

我想做的是评论:

type foo struct {
  Message string `json:"message"`
}

func bar() {
  //The "message" field contains a bool type which does not 
  //match the equivalent "message" field in foo, a string type
  jsonData := byte[]("{
    \"message\": true
  }")
  var baz foo
  //Because the type of "message" in the json bytes is 
  //bool and not a string, put "" inside baz.Message
  json.Unmarshal(jsonData, &baz) 
}

如何解组json字节数组,然后仅在字段与json字节数组字段中的类型匹配时填充特定字段?如果字段类型与json字节数组字段类型不匹配,请输入一个占位符值nil,“,”等?

一旦找到,有很多方法可以处理这个问题。我喜欢的一种方法是将其解组为
interface{}
类型的变量,然后可以检查该变量:

package main

import (
    "encoding/json"
    "fmt"
)

type foo struct {
    Message interface{} `json:"message"`
}

func bar() {
    //The "message" field contains a bool type which does not
    //match the equivalent "message" field in foo, a string type
    jsonData := []byte(`{
    "message": true
  }`)
    var baz foo
    //Because the type of "message" in the json bytes is
    //bool and not a string, put "" inside baz.Message
    err := json.Unmarshal(jsonData, &baz)
    fmt.Printf("unmarhal error is: %v\n", err)
    if err == nil {
        fmt.Printf("baz.Message is now: %T = %v\n", baz.Message, baz.Message)
    }
}

func main() {
    bar()
}
()


现在应该很清楚如何打开类型(解码后),看看你得到的是否是你想要的。如果是,就用它。如果没有,请使用默认设置。如有必要,首先将传入的json解码为更通用的Go类型,然后填写您真正想要处理的特定类型。

一旦您成功,有很多方法可以处理此问题。我喜欢的一种方法是将其解组为
interface{}
类型的变量,然后可以检查该变量:

package main

import (
    "encoding/json"
    "fmt"
)

type foo struct {
    Message interface{} `json:"message"`
}

func bar() {
    //The "message" field contains a bool type which does not
    //match the equivalent "message" field in foo, a string type
    jsonData := []byte(`{
    "message": true
  }`)
    var baz foo
    //Because the type of "message" in the json bytes is
    //bool and not a string, put "" inside baz.Message
    err := json.Unmarshal(jsonData, &baz)
    fmt.Printf("unmarhal error is: %v\n", err)
    if err == nil {
        fmt.Printf("baz.Message is now: %T = %v\n", baz.Message, baz.Message)
    }
}

func main() {
    bar()
}
()


现在应该很清楚如何打开类型(解码后),看看你得到的是否是你想要的。如果是,就用它。如果没有,请使用默认设置。如有必要,首先将传入的json解码为更通用的Go类型,然后填写您真正想要处理的特定类型。
encoding/json
包中的
Unmarshaler
接口允许您完全控制如何将数据解码为您的类型

您只需要在类型上实现一个方法-
解组JSON(数据[]字节)

在下面的示例中,我在
UnmarshalJSON
方法中声明了一个临时匿名类型,并将
json
解码为一个空接口

然后,我可以将assert接口键入一个
字符串
,成功后,我在原始
Foo
类型上设置
消息
字段。我们不需要处理失败的情况,因为Go中的字符串有一个默认值


encoding/json
包中的
Unmarshaler
接口允许您完全控制如何将数据解码为您的类型

您只需要在类型上实现一个方法-
解组JSON(数据[]字节)

在下面的示例中,我在
UnmarshalJSON
方法中声明了一个临时匿名类型,并将
json
解码为一个空接口

然后,我可以将assert接口键入一个
字符串
,成功后,我在原始
Foo
类型上设置
消息
字段。我们不需要处理失败的情况,因为Go中的字符串有一个默认值


你不能,不能用标准库和Go的内置类型。关于哪些json类型可以解组为哪些Go类型的规则可以在上的文档中找到。但是,您可以声明一个自定义类型,让该类型实现
json.Unmarshaler
接口,并在该自定义实现中设置自己的规则。编码/json包不支持将bool存储到字符串中。也许这个领域应该改为一个布尔?此外,JSON无效;它已经在那里了。但是,如果使用正确的JSON,您很可能会收到错误消息(检查错误Unmarshal返回!),即无法将布尔值解组到go类型字符串。在进入语义之前,正确使用语法确实很有帮助:-)下面是一个示例;试试看。那么。现在你准备好问一个问题了!你不能,不能用标准库和Go的内置类型。关于哪些json类型可以解组为哪些Go类型的规则可以在上的文档中找到。但是,您可以声明一个自定义类型,让该类型实现
json.Unmarshaler
接口,并在该自定义实现中设置自己的规则。编码/json包不支持将bool存储到字符串中。也许这个领域应该改为一个布尔?此外,JSON无效;它已经在那里了。但是,如果使用正确的JSON,您很可能会收到错误消息(检查错误Unmarshal返回!),即无法将布尔值解组到go类型字符串。在进入语义之前,正确使用语法确实很有帮助:-)下面是一个示例;试试看。那么。现在你准备好问一个问题了!