aliyun / alibaba-cloud-sdk-go

Alibaba Cloud SDK for Go
Apache License 2.0
1.16k stars 273 forks source link

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

Open krisice opened 1 year ago

krisice commented 1 year ago

我在使用 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) 这样的判断。但是我觉得这样也太麻烦了吧。