1

I have a json like this:

{
"user": [
{
  "det": [
    {
      "Code": "9",
      "Description": "Update"
    }
  ],
  "arts": []
 }
]
}

Now in some case the json that I reiceve contains the key arts in other case this key isn't provided, so simply I have only the json with the det key as:

{
"user": [
{
  "det": [
    {
      "Code": "9",
      "Description": "Update"
    }
  ]
 }
]
}

and this is my class for deserializing it:

public class Det
{
   public string Code { get; set; }
   public string Description { get; set; }
}

public class Arts
{
   public string ID { get; set; }
   public string CON3 { get; set; }
}

public class user
{
   public List<Det> det { get; set; }
   public List<Arts> arts {get; set; }
}

public class RootObject
{
    public List<Det> user { get; set; }
}

So when I receive the first json the code working well, but when I receive the second the code fall in exception:

Cannot deserialize the current JSON object

This because the arts key isn't provided but there is only the det key. So there is a way to tell to my code to ignore arts if is not setted? How can I do this? Thanks.

Quantic
  • 1,779
  • 19
  • 30
Sandokan
  • 1,075
  • 3
  • 15
  • 30
  • What are you using to deserialize? Using google, I found [this](http://stackoverflow.com/questions/20578846/ignore-missing-properties-during-jackson-json-deserialize-in-java) if you are using "`Jackson`", and [this](http://stackoverflow.com/questions/30928903/json-how-to-ignore-missing-object-during-deserialization) for "`Json.net`". – Quantic Apr 06 '16 at 15:58
  • @Quantic I'm using `Json.net` I'll take a look to the seconds link. – Sandokan Apr 06 '16 at 15:58
  • Ok I modified the second link to a more relevant one, so double check it in case you checked before I changed it. – Quantic Apr 06 '16 at 16:01
  • @Quantic I tried to put this property: `[JsonProperty(Required = Required.AllowNull)]` above this line: `public List arts {get; set; }` but still get the same error. It's not possible manage it directly in the class declaration? – Sandokan Apr 06 '16 at 16:03
  • Can you post the code you are using to deserialize? I just tried `var test1 = JsonConvert.DeserializeObject(json2);` and it works fine. In this case `json2` is a string that is a copy if your second json code you posted. – Quantic Apr 06 '16 at 16:19
  • @Quantic My bad. I tried with: `JsonProperty("arts", NullValueHandling = NullValueHandling.Ignore)]` and the deserialization seems working. Thanks for the help, I have appreciated. Just one question: when I execute (in my foreach) this condition ` if (det.arts.Count > 0)` I get: `NullReferenceException` this 'cause `arts` is null, how can I prevent this situation? – Sandokan Apr 06 '16 at 16:28
  • @Sandokan - check [What is a NullReferenceException and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Simplest thing to do is to check for null. – dbc Apr 06 '16 at 16:40
  • yeah I execute the check but still get the error maybe I'll open a new question. – Sandokan Apr 06 '16 at 16:43
  • @Sandokan - you have a bug in your `RootObject` class. It should be `public List user { get; set; }` not `public List user { get; set; }`. – dbc Apr 06 '16 at 16:47
  • Yes, @dbc I noticed it. Anyway for the related value `null` I opened a new question: https://stackoverflow.com/questions/36457413/cant-check-if-a-specific-object-key-is-null – Sandokan Apr 06 '16 at 16:49

0 Answers0