json - 我如何解码这个 JSON

标签 json go

我有两个具有以下签名的函数:

 1)  func printAWSDataToConsole(edgexcontext *appcontext.Context, params ...interface{}) (bool, interface{}) {


  2)   func (f Conversion) TransformToAWS()

在哪里
func NewConversion() Conversion {
    return Conversion{}
}

起初 TransformToAWS 正在返回
      true, string(msg)

我可以通过做一个简单的来打印 printAWSDataToConsole 中 msg 的值
     fmt.Println(params[0].(string))

现在我更改了 TransformToAWS 以返回一个 json:
    data := YuccaDataStream {
                 Stream : "temperatura2",
                 Sensor : "ec6c613a-66b4-4584-fb37-5f7cac130f7d",
                 Values : []Value {
                           Value {
                           Time:formattedTime,
                           Components: Components{Alfanum :"15.55"},
       }, }, }

     dataAsBytes, err := json.Marshal(data)

     return true, dataAsBytes

在哪里
type YuccaDataStream struct {
    Stream string `json:"stream"`
    Sensor string `json:"sensor"`
    Values []Value `json:"values"`
    }

type Value struct {
    Time string `json:"time"`
    Components `json:"components"`
}

type Components struct {
    Alfanum string `json:"alfanum"`
}

我不知道如何在 printAWSDataToConsole 中解码 json。

最佳答案

要解码,请使用以下代码:

var dataOut YuccaDataStream
err = json.Unmarshal(dataAsBytes, &dataOut)

就是这样。就如此容易。

关于json - 我如何解码这个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59154233/

相关文章:

go - Gin 错误 : listen tcp: lookup addr on : no such host

go - GoLand找不到引用: “Unresolved reference ' NewRGBA' ”

go - binary.Write() 字节排序不适用于 []byte

json - Slick Slider 未使用 API json 初始化

javascript - JSON 用户搜索

json - 使用 jsonpath 查询从 Json 对象/数组中过滤名称

go - 尝试同步 goroutine 时出现死锁错误

python - 嵌套字典的下键和合并数据(如果下键已经存在)

javascript - 从给定数据生成 json 对象

卡在 Windows 10 上的 Go 程序