aliyun / alibaba-cloud-sdk-go

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

[SDK.JsonUnmarshalError] caused by:cms.DescribeSiteMonitorDataResponse.Data: fuzzyStringDecoder: not number or string or bool, error found in #10 byte of #512

Closed boke168 closed 3 years ago

boke168 commented 3 years ago

问题描述

SDK版本为v1.61.1127,示例代码为官方sdk demo代码,运行到response, err := client.DescribeSiteMonitorData(request)这行代码报错,err 总是不等于 nil,无法正常解析接口数据。

示例代码

package main

import (
        "fmt"
        "os"
        cms "github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
        "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

func main() {
        client, err := cms.NewClientWithAccessKey("cn-hangzhou", os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))

        request := cms.CreateDescribeSiteMonitorDataRequest()
        request.Scheme = "https"

        request.TaskId = "df358848-e202-41be-a446-xxxxxxxxxxxx"
        request.MetricName = "Availability"

        response, err := client.DescribeSiteMonitorData(request)
        if err != nil {
                fmt.Print(err.Error())
        }
        fmt.Printf("response is %#v\n", response)
}

运行结果

shell@Alicloud:~/alibabacloud_sdk_demo/cms/wylgemn-8/go$ go run ./main/DescribeSiteMonitorData.go
[SDK.JsonUnmarshalError] Failed to unmarshal response, but you can get the data via response.GetHttpStatusCode() and response.GetHttpContentString()
caused by:
cms.DescribeSiteMonitorDataResponse.Data: fuzzyStringDecoder: not number or string or bool, error found in #10 byte of ...|9","Data":[{"Over2to|..., bigger context ...|sb3RzIjpbMTYyNjY1MjgwMF0sIndpbmRvdyxxxxxx","Data":[{"Over2totalTime":0.0,"Error4XXRate":0.0,"Over100|...response is &cms.DescribeSiteMonitorDataResponse{BaseResponse:(*responses.BaseResponse)(0xc000068700), Code:"", Message:"successful", Success:"", RequestId:"8B8D1D75-075A-41A6-8968-xxxxxxxxxxxx", NextToken:"eyJidWNrZXRCeXRlcyI6IlFBPT0iLCJidWNrZXRzIjo0LCJjdXJyZW50Q......", Data:""}

报错信息

[SDK.JsonUnmarshalError] Failed to unmarshal response, but you can get the data via response.GetHttpStatusCode() and response.GetHttpContentString() caused by:cms.DescribeSiteMonitorDataResponse.Data: fuzzyStringDecoder: not number or string or bool, error found in #10 byte of

JacksonTian commented 3 years ago

更新的 SDK 的版本看看是否解决该问题。

该问题的背后原因是 API 声明的类型与实际返回的类型不一致导致的。

boke168 commented 3 years ago

@JacksonTian 试了最新版本的SDK,报错和上面相同。

go.mod

module main.go
go 1.16
require github.com/aliyun/alibaba-cloud-sdk-go v1.61.1184

go.sum

github.com/aliyun/alibaba-cloud-sdk-go v1.61.1184 h1:okHArB9uvBnZGVXAZYWEeNtJJL/Nd9rUlLCaSnKrjtg=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1184/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
boke168 commented 3 years ago

@JacksonTian 我尝试修改SDK中Data的数据结构,将Data改为数组类型,运行不再报错了。希望SDK可以及时更新,谢谢。

// DescribeSiteMonitorDataResponse is the response struct for api DescribeSiteMonitorData
type DescribeSiteMonitorDataResponse struct {
    *responses.BaseResponse
    Code      string `json:"Code" xml:"Code"`
    Message   string `json:"Message" xml:"Message"`
    Success   string `json:"Success" xml:"Success"`
    RequestId string `json:"RequestId" xml:"RequestId"`
    NextToken string `json:"NextToken" xml:"NextToken"`
    Data      []metricData
}

type metricData struct {
    Availability float32 `json:"Availability"`
    AvailableNumber  int `json:"AvailableNumber"`
    UnavailableNumber int `json:"UnavailableNumber"`
}
boke168 commented 3 years ago

请注意: Success字段返回值为bool类型 AvailableNumber等字段返回值为float类型

type DescribeSiteMonitorDataResponse struct {
    *responses.BaseResponse
    Code      string `json:"Code" xml:"Code"`
    Message   string `json:"Message" xml:"Message"`
    Success   bool   `json:"Success" xml:"Success"`
    RequestId string `json:"RequestId" xml:"RequestId"`
    NextToken string `json:"NextToken" xml:"NextToken"`
    Data      []metricData  `json:"Data" xml:"Data"`
}
type metricData struct {
    Availability float32 `json:"Availability" xml:"Availability"`
    AvailableNumber  float32 `json:"AvailableNumber" xml:"AvailableNumber"`
    UnavailableNumber float32 `json:"UnavailableNumber" xml:"UnavailableNumber"`
}

问题得到临时解决,故关闭问题。如需继续讨论请在后面回复。谢谢。