-1

当我尝试反序列化响应数据时,我收到了错误

“无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'CModellassType',因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化。"

string responseData = responseCategory.Content.ReadAsStringAsync().Result;
UploadApiResponse UploadApiResponseModel = JsonConvert.DeserializeObject<UploadApiResponse>(responseData);
public class UploadApiResponse
{
    public int StatusCode { get; set; }

    public string Message { get; set; }

    public bool IsError { get; set; }

    public Data Data { get; set; }
}

public class Data
{
    public string AccessToken { get; set; }
}
{
   "StatusCode":1,
   "Message":"Invalid Parameter.",
   "IsError":true,
   "Data":[
      "CurrencyCode is empty or (greater then 3 or less then 3)"
   ]
}
4

1 回答 1

0

JSON 与您的模型不匹配。您的 JSON 正在传递 Data 对象的字符串集合。

为了使您当前的 JSON 正确反序列化,您需要将 UploadApiResponse 类修改为以下内容:

public class UploadApiResponse
{
  public int StatusCode { get; set; }
  public string Message { get; set; }
  public bool IsError { get; set; }
  public string[] Data { get; set; }
}

查看您在 JSON 的 Data 元素中传递的数据,您似乎正在尝试传递更多异常详细信息,而不是 AccessToken。

如果您有权修改 JSON,则可以将其修改为以下内容:

{
  "StatusCode":1,
  "Message":"Invalid Parameter",
  "IsError":true,
  "Data": {
    "InnerException":"CurrencyCode is empty or (greater then 3 or less then 3)",
  "AccessToken": ""
  }
}

然后,您可以按如下方式更新模型:

public class UploadApiResponse
    {
        public int StatusCode { get; set; }
        public string Message { get; set; }
        public bool IsError { get; set; }
        public Data Data { get; set; }
    }

    public class Data
    {
        public string InnerException { get; set; }
        public string AccessToken { get; set; }
    }
于 2021-06-02T12:45:52.530 回答