Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing Json into ObservableCollection

Tags:

c#

json.net

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?

like image 713
DreamNet Avatar asked Mar 20 '23 23:03

DreamNet


1 Answers

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:

  1. change your JSON to eliminate the extra layer.
  2. define a wrapper class (e.g. EmployeeHolder) to represent the extra layer in the JSON, and make FooEmployees a collection of those.
  3. implement a JSON converter to abstract away the extra layer during deserialization. See Is there a way to deserialize my JSON without using a wrapper class for each item? for more details on this approach.
like image 71
Brian Rogers Avatar answered Mar 31 '23 14:03

Brian Rogers