go - 如何在 GoLang 中找到 json.Unmarshalled() 对象的长度?

标签 go marshalling unmarshalling

["contexts"]我有这个 GoLang 代码:

1:      unmarshalledBody := make(map[string]interface{})
2:      err = json.Unmarshal(someData, &unmarshalledBody)
3:      fmt.Println(unmarshalledBody["some-key"])
4:      fmt.Println(len(unmarshalledBody["some-key"]))
第 3 行导致此输出:
[map[A:R B:T C:V] map[A:S B:U C:W]]
第 4 行导致此错误:
invalid argument unmarshalledBody (type interface {}) for len
那么我怎样才能找出未编码对象的长度呢?

最佳答案

func len(v 类型) int
len 内置函数根据类型返回 v 的长度:

Array: the number of elements in v.
Pointer to array: the number of elements in *v (even if v is nil).
Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
String: the number of bytes in v.
Channel: the number of elements queued (unread) in the channel buffer;
     if v is nil, len(v) is zero.
https://golang.org/src/builtin/builtin.go?s=5935:5955#L148
建议:
val, ok := unmarshalledBody["some-key"].(type)

关于go - 如何在 GoLang 中找到 json.Unmarshalled() 对象的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64055918/

相关文章:

json - 从 golang Post Form 获取动态数组

testing - golang中如何测试并发和锁?

c# - 如何从 C# 调用采用指向 BSTR 的指针的 COM 方法

c# - 如何编码多字节字符集 C#

golang中的json解码用于复杂的json数据

go - 如何将 *int64 转换为 *int32?

c# - 在 C# 中的 C/C++ DLL 中使用嵌套结构指针

java - 如何使用不同的标签解码 xml

json - 在 Go 中解码时如何识别无效值和未指定的字段?

go - 如何干净地处理结构状态的错误?