I'm trying to deserialize this Json using Json.Net lib.:-
{
"Employees": [
{
"Employee": {
"Name": "AAA",
"Info": [
{
"Signature": "aaa"
},
{
"Group": "AaA"
},
{
"E-mail": "aAa"
},
{
"Tfn home": "1234"
},
{
"Tfn mobile": "1324"
},
{
"Tfn work": "1234"
},
{
"Tfn pager": "1234"
}
]
}
},
{
"Employee": {
"Name": "BBB",
"Info": [
{
"Signature": "bbb"
},
{
"Group": "BbB"
},
{
"E-mail": "bBb"
},
{
"Tfn home": "1234"
},
{
"Tfn mobile": "1234"
},
{
"Tfn work": "1234"
},
{
"Tfn pager": "1234"
}
]
}
}
]
}
Into the following:-
public class Foo
{
private ObservableCollection<Employee> _fooEmployees = new ObservableCollection<Employee>();
public ObservableCollection<Employee> FooEmployees
{
get { return _fooEmployees; }
set { _fooEmployees = value; }
}
}
[JsonObject]
public class Employee
{
[JsonProperty(PropertyName = "Name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "Info")]
private ObservableCollection<Infomation> _infoList = new ObservableCollection<Infomation>();
public ObservableCollection<Infomation> InfoList
{
get { return _infoList; }
set
{
_infoList = value;
}
}
}
[JsonDictionary]
public abstract class Infomation
{
[JsonProperty(PropertyName = "Signature")]
public string Signature { get; set; }
[JsonProperty(PropertyName = "Group")]
public string Group { get; set; }
[JsonProperty(PropertyName = "E-mail")]
public string Email { get; set; }
[JsonProperty(PropertyName = "Tfn home")]
public string TfnHome { get; set; }
[JsonProperty(PropertyName = "Tfn mobile")]
public string TfnMobile { get; set; }
[JsonProperty(PropertyName = "Tfn work")]
public string TfnWork { get; set; }
[JsonProperty(PropertyName = "Tfn pager")]
public string TfnPager { get; set; }
}
Using the following line of code:-
var kol = JsonConvert.DeserializeObject<Foo>(json);
The problem is that Kol return FooEmployees with 0 Count.
Anyone who can direct me to why it is not working?
You're getting an empty collection because your classes don't match your JSON. I see at least two issues:
First, the outer JSON object has a property called Employees
but the Foo
class you are deserializing into has a property called FooEmployees
. Since the names don't match, your FooEmployees
collection will remain empty. Try adding a [JsonProperty("Employees")]
attribute to the FooEmployees
property like you did elsewhere in your code.
Second, the Employees
collection in the JSON does not actually contain a collection of Employee
objects as you have defined them in your classes. Instead, it contains a collection of objects that each have single property called Employee
which contains an Employee
instance. To fix this, you can do one of the following:
EmployeeHolder
) to represent the extra layer in the JSON, and make FooEmployees
a collection of those.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With