-1

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)"
   ]
}
demo
  • 6,038
  • 19
  • 75
  • 149
  • 2
    "not able to do Convert.DeSerialize" - what's wrong? Give more details what you get in result? Any errors? Show what is `UploadApiResponse` – demo Jun 02 '21 at 07:08
  • 1
    note: better to use `async/await` for async requests – demo Jun 02 '21 at 07:11
  • hi, 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." – Azaz ahmad Tapadar Jun 02 '21 at 07:53
  • then looks like you need to update model `UploadApiResponse` according to your JSON. Please show what is `UploadApiResponse` – demo Jun 02 '21 at 08:18
  • 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; } } – Azaz ahmad Tapadar Jun 02 '21 at 08:25
  • as you may see `Data` in your JSON is of different type than you have in your model. in your JSON it is of type `string[]` – demo Jun 02 '21 at 09:19
  • so how to overcome this issue? kindly suggest. – Azaz ahmad Tapadar Jun 02 '21 at 09:40
  • 2
    @AzazahmadTapadar `public string[] Data { get; set; }` because `Data` is a collection not an object. – Peter Csala Jun 02 '21 at 10:02
  • @AzazahmadTapadar probably for a case when you aren't receiving error, your model is correct... but in case when response contains error, it isn't correct. Am I right? – demo Jun 02 '21 at 11:15
  • here you can see the response contains both string and array type. If is it a List type then easily deserialization could have done. "{\"StatusCode\":1,\"Message\":\"Invalid Parameter.\",\"IsError\":true,\"Data\":[\"CurrencyCode is empty or (greater then 3 or less then 3)\"]}" – Azaz ahmad Tapadar Jun 02 '21 at 11:55

1 Answers1

0

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; }
    }
Kev Ritchie
  • 1,599
  • 10
  • 16