'Deserialized Object Has All Values Set to Null
I'm trying to deserialize JSON into a custom object but all my properties are set to null and not sure what's going on. Does anyone see anything 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; }
}
Solution 1:[1]
Your JSON has an outer object which 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);
}
Solution 2:[2]
check if the destination type has internal (or private) set modifier for those properties, even if the property has public access modifier itself.
public class Summary{
public Class2 Prop1 { get; internal set; }
public Class1 prop2 { get; set; }
}
Solution: remove those explicit internal access modifier and make them writable from outside
Solution 3:[3]
The problem here is that you're defining Keys as just a class, when it's 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; }
}
Solution 4:[4]
You don't need to define PropertiesList as a list in the class just call the PropertiesList Class as 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 to deserialize:
keys myDeserializedObjValid = JsonConvert.DeserializeObject<keys>(strResponseValid);
Solution 5:[5]
JSON.NET is an opt-in serialization library. The properties in your objects require attributes in order to flag 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; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank |
Solution 2 | |
Solution 3 | Mike Richards |
Solution 4 | |
Solution 5 | Ricky Smith |