not able to do Convert.Deserialize

164 views Asked by At

When i am trying to Deserialized Response data i am getting the error

"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CModellassType' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly."

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)"
   ]
}
1

There are 1 answers

0
Kev Ritchie On

There is a mismatch between the JSON and your model. Your JSON is passing in a collection of strings for the Data object.

For your current JSON to deserialize correctly, you'll need to amend your UploadApiResponse class to the following:

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

Looking at the data you're passing in the Data element of the JSON, it looks like you're trying to pass further exception details, as opposed to an AccessToken.

If you have access to amend the JSON, you could amend it to the following:

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

You can then update your models as follows:

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; }
    }