For a deserialized object, all values ​​are set to zero

I am trying to deserialize JSON into a custom object, but all my properties are null and not sure what is going on. Does anyone see something wrong?

JSON example

{ "Keys": [ { "RegistrationKey": "asdfasdfa", "ValidationStatus": "Valid", "ValidationDescription": null, "Properties": [ { "Key": "Guid", "Value": "i0asd23165323sdfs68661358" } ] } ] } 

Here is my code where strResponseValid is the JSON above.

 Keys myDeserializedObjValid = (Keys)JsonConvert.DeserializeObject(strResponseValid, typeof(Keys)); validationStatusValid = myDeserializedObjValid.ValidationStatus; 

Here are my classes

  public class Keys { public string RegistrationKey { get; set; } public string ValidationStatus { get; set; } public string ValidationDescription { get; set; } public List<Properties> PropertiesList { get; set; } } public class Properties { public string Key { get; set; } public string Value { get; set; } } 
+4
source share
5 answers

Your JSON has an external object that contains a collection of Key objects. The following code works (I tested it):

  class KeyWrapper { public List<Key> Keys { get; set; } } class Key { public string RegistrationKey { get; set; } public string ValidationStatus { get; set; } public string ValidationDescription { get; set; } public List<Properties> Properties { get; set; } } public class Properties { public string Key { get; set; } public string Value { get; set; } } public void DeserializeKeys() { const string json = @"{""Keys"": [ { ""RegistrationKey"": ""asdfasdfa"", ""ValidationStatus"": ""Valid"", ""ValidationDescription"": null, ""Properties"": [ { ""Key"": ""Guid"", ""Value"": ""i0asd23165323sdfs68661358"" } ] } ] }"; var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapper>(json); } 
+4
source

in my case, it was because my destination type has internal (or private) set modifiers for these properties.

 public class Summary{ public QuickStats Stats { get; internal set; } public QuickStats Stats2 { get; set; } } 

after removing the internal modifier json.net also deserializes these objects, as in the serialization stage

+5
source

You do not need to define a PropertiesList as a list in a class, just call the PropertiesList class, as shown below.

 public class Keys { public string RegistrationKey { get; set; } public string ValidationStatus { get; set; } public string ValidationDescription { get; set; } public PropertiesList PropertiesList { get; set; } } public class PropertiesList { public string Key { get; set; } public string Value { get; set; } } 

Then try using the following for deserialization:

 keys myDeserializedObjValid = JsonConvert.DeserializeObject<keys>(strResponseValid); 
+1
source

The problem is that you define Keys as just a class when it is actually a property.

 public class Response { public Keys Keys { get; set; } } public class Keys { public string RegistrationKey { get; set; } public string ValidationStatus { get; set; } public string ValidationDescription { get; set; } public List<Properties> PropertiesList { get; set; } } public class Properties { public string Key { get; set; } public string Value { get; set; } } 
+1
source

JSON.NET is an input / output library. The properties of your objects require attributes to mark them as being included in the JSON structure.

 public class Keys { [JsonProperty(PropertyName = "RegistrationKey")] public string RegistrationKey { get; set; } [JsonProperty(PropertyName = "ValidationStatus")] public string ValidationStatus { get; set; } [JsonProperty(PropertyName = "ValidationDescription")] public string ValidationDescription { get; set; } [JsonProperty(PropertyName = "Properties")] public List<Properties> PropertiesList { get; set; } } public class Properties { [JsonProperty(PropertyName = "Key")] public string Key { get; set; } [JsonProperty(PropertyName = "Value")] public string Value { get; set; } } 
0
source

All Articles