json 格式改變

来源:13-4 第三方API数据格式的解析技巧

weixin_慕标4382377

2022-04-14

阿里巴巴格式改變了,主要是Data多了struct但編譯時
panic: json: cannot unmarshal string into Go struct field .data of type []struct { Result []main.result “json:“result””; Success string “json:“success”” }


type result struct {
synonym string json:"synonym"
weight string json:"wight"
tag string json:"tag"
word string json:"word"
}

func parseNLP() {
res := {"RequestId":"FA53D08F-37D1-4D81-BEE7-41F24E825F60","Data":"{\"result\":[{\"synonym\":\"\",\"weight\":\"0.100000\",\"tag\":\"普通词\",\"word\":\"请\"},{\"synonym\":\"\",\"weight\":\"0.100000\",\"tag\":\"普通词\",\"word\":\"输入\"},{\"synonym\":\"\",\"weight\":\"1.000000\",\"tag\":\"品类\",\"word\":\"文本\"}],\"success\":true}"}

m := struct {
	RequestId string `json:"id"`
	Data      struct {
		Result  []result `json:"result"`
		Success string   `json:"success"`
	} `json:"data"`
}{}
err := json.Unmarshal([]byte(res), &m)
if err != nil {
	panic(err)
}
fmt.Printf("%+v\n", m)

}

写回答

1回答

merlin丶kael

2022-04-24

你这个res 的 Data 不是个json 类型 是string类型了

还有 应该不用 `json:data` 了, 这个字符串 就是 大写的 Data

0
0

Google资深工程师深度讲解Go语言 由浅入深掌握Go语言

语法+分布式爬虫实战 为转型工程师量身打造

5995 学习 · 1909 问题

查看课程