Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response.ContentString() 这个函数返回的字符串是不是格式有误? #594

Open
krisice opened this issue Jun 23, 2023 · 0 comments

Comments

@krisice
Copy link

krisice commented Jun 23, 2023

我在使用 NLP 的计算文本相似度的计算 golang API 过程中,为什么 response.ContentString() 这个返回的字符串,字符串打印的结果为

"{\"RequestId\":\"B813EE57-B39C-5CF7-8C1E-9FDB25E75105\",\"Data\":\"{\\\"result\\\":[{\\\"score\\\":\\\"0.46740812\\\",\\\"flag\\\":true}],\\\"success\\\":true}\"}"

这个字符串完全用 json.Unmarshal 解析不出来对应的 struct 结果。

type Response struct {
	RequestID string `json:"RequestId"`
	Data      struct {
		Result []struct {
			Score float64 `json:"score"`
			Flag  bool    `json:"flag"`
		} `json:"result"`
		Success bool `json:"success"`
	} `json:"Data"`
}

我自己试着自己写了个 fake response string

fakeResponseString := `{
		"RequestId": "sdsds",
		"Data": {
			"result":[
				{
					"score": 23.0,
					"flag": true
				}
			],
			"success": true
		}
}

这个 json.Unmarshal 能够解析出正确的 Response struct 结果。我不太清楚是我使用错误还是其它什么原因。
我也尝试过一步一步解析这个你们返回的 response string,使用多次的类型推断,例如 golang 中的 responseString.(type) 这样的判断。但是我觉得这样也太麻烦了吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant