如何将值反序列化为类型为map[string][int]的JSON结构体?

huangapple go评论80阅读模式
英文:

How to Unmarshal value into json struct of type map[string][int]

问题

在对某个API的resp.body执行ioutil.ReadAll之后,我得到了以下结果:

[91 34 123 92 34 78 111 79 102 86 105 101 119 115 92 34 58 123 92 34 48 92 34 58 48 125 44 92 34 78 111 79 102 76 105 107 101 115 92 34 58 123 92 34 48 92 34 58 48 125 44 92 34 78 111 79 102 67 111 109 109 101 110 116 115 92 34 58 123 92 34 48 92 34 58 48 125 44 92 34 78 111 79 102 83 104 97 114 101 115 92 34 58 123 92 34 48 92 34 58 48 125 125 34 44 34 123 125 34 93 10]

现在我需要将其解组为类型为:

type ActUser struct {
NoOfViews map[string]int json:"NoOfViews,omitempty"
NoOfLikes map[string]int json:"NoOfLikes,omitempty"
NoOfComments map[string]int json:"NoOfComments,omitempty"
NoOfShares map[string]int json:"NoOfShares,omitempty"
}

但是当我执行以下操作时:

var try []ActUser
err = json.Unmarshal(bodyBytes, &try)

我得到错误:cannot unmarshal string into Go value of type model.ActUser

我尝试过转换,但似乎没有起作用。

英文:

After doing ioutil.ReadAll on a resp.body of some API ,I'm getting:-

[91 34 123 92 34 78 111 79 102 86 105 101 119 115 92 34 58 123 92 34 48 92 34 58 48 125 44 92 34 78 111 79 102 76 105 107 101 115 92 34 58 123 92 34 48 92 34 58 48 125 44 92 34 78 111 79 102 67 111 109 109 101 110 116 115 92 34 58 123 92 34 48 92 34 58 48 125 44 92 34 78 111 79 102 83 104 97 114 101 115 92 34 58 123 92 34 48 92 34 58 48 125 125 34 44 34 123 125 34 93 10]

Now I've to unmarshal this to a struct json of type =

type ActUser struct {
	NoOfViews    map[string]int `json:"NoOfViews,omitempty"`
	NoOfLikes    map[string]int `json:"NoOfLikes,omitempty"`
	NoOfComments map[string]int `json:"NoOfComments,omitempty"`
	NoOfShares   map[string]int `json:"NoOfShares,omitempty"`
}

But when I do

var try []ActUser
err = json.Unmarshal(bodyBytes, &try)

I'm getting error := cannot unmarshal string into Go value of type model.ActUser

I tried converting,but nothing seems to work.

答案1

得分: 4

你的示例JSON数据[91 34 123 ...对应的是["{

这表明你收到的JSON可能是无效的——它是一个字符串数组,而不是对象数组。看起来你的对象在编组时可能被引号括起来了。

它可以被解组为[]string,而不是[]ActUser。这几乎肯定是不希望的,在源数据编码时出现了错误。最好的方法是修复导致JSON对象被引号括起来的错误。

或者,如果你必须从有问题的JSON中提取数据,你可以使用以下代码:

var strs []string
if err := json.Unmarshal(bodyBytes, &strs); err != nil {
  log.Fatal(err)
}

if len(strs) == 0 {
  log.Fatal("missing ActUser object")
}

var user ActUser
if err := json.Unmarshal([]byte(strs[0]), &user); err != nil {
  log.Fatal(err)
}

另外,我建议你使用fmt.Printf("%s\n", bodyBytes)来显示你的原始JSON数据进行调试(比显示ASCII码列表要简单得多)。

英文:

Your example JSON data [91 34 123 ... corresponds to ["{.

This indicates the JSON you are receiving is probably invalid -- it's an array of strings, not an array of objects. It looks like your object is probably getting quoted when it is marshalled.

It can be unmarshalled into []string, not []ActUser. This is almost certainly undesirable, and a mistake when the source data was encoded. The best approach would be to fix the bug which causes the JSON object to be quoted as a string.

Alternatively, if you must extract data from the buggy JSON, you could:

var strs []string
if err := json.Unmarshal(bodyBytes, &strs); err != nil {
  log.Fatal(err)
}

if len(strs) == 0 {
  log.Fatal("missing ActUser object")
}

var user ActUser
if err := json.Unmarshal([]byte(strs[0]), &user); err != nil {
  log.Fatal(err)
}

Separately, I'd recommend using fmt.Printf("%s\n", bodyBytes) to display your raw JSON data for debugging (much easier than a list of ASCII codes).

huangapple
  • 本文由 发表于 2021年9月23日 22:50:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/69302317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定