0

I get cannot deserialize error when the items in the json list is 1 , it works when the items are > 1

This is my json

  {
   "avetmiss": {

  "disabilities": {
    "disability": [
      {
        "disability-type": "Physical"
      },
      {
        "disability-type": "Hearing/Deaf"
      }
    ]
  },
  "prior-educations": {
    "prior-education": {
      "id": "2",
      "prior-education-type": "Bachelor Degree or Higher Degree level"
    }
  }
}}

and this is the class with which I deserialize

 public class Root {
     public PriorEducations prioreducations { get; set; }
    [JsonProperty("disabilities")]
    public Disabilities disabilities { get; set; }
}

public class Disabilities
{
    [JsonProperty("disability")]
    public List<DisabilityType> disability { get; set; }
}

public class DisabilityType
{
    [JsonProperty("disability-type")]
    public string disabilitytype { get; set; }
}

public class PriorEducations
{
    [JsonProperty("prior-education")]
    public List<PriorEducation> prioreducation { get; set; }
}

public class PriorEducation
{
    [JsonProperty("id")]
    public string id { get; set; }
    [JsonProperty("prior-education-type")]
    public string prioreducationtype { get; set; }
}

So basically it works with Disabilities but not with prior education (because the items in Disabilities are >1 where as prior edu.. is 1 ) It works if I change the prioreducation[] to prioreducation

Bajju
  • 925
  • 11
  • 27
  • Show the json for `list is 1` case . I guess it is not an array `[]` but an object `{}` – EZI Jul 18 '15 at 01:14
  • Hey EZI Can you give me a sample please . – Bajju Jul 18 '15 at 01:45
  • Refresh this page and see the answer marked as duplicate.. Keyword is: *writing a custom JsonConverter* – EZI Jul 18 '15 at 01:47
  • Hey EZI , tried the solution from L.B but I get the reader.value as null . – Bajju Jul 18 '15 at 02:14
  • 1
    See also http://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n which may be a better match for this question. – dbc Jul 18 '15 at 17:06
  • Thanks dbc , I found that solution to be the perfect one as it uses a templated type and works with any object. – Bajju Jul 20 '15 at 02:02

0 Answers0