Newtonsoft.Json serialization returns empty json object

asp.netJsonjson.net

asp.net Problem Overview


I have list of objects of following class:

public class Catagory
{
    int catagoryId;
    string catagoryNameHindi;
    string catagoryNameEnglish;
    List<Object> subCatagories;
    public Catagory(int Id, string NameHindi, string NameEng,List<Object> l)
    {
        this.catagoryId = Id;
        this.catagoryNameHindi = NameHindi;
        this.catagoryNameEnglish = NameEng;
        this.subCatagories = l;
    }
}

  public class SubCatagory
{
    int subCatagoryId { get; set; }
    string subCatNameHindi { get; set; }
    string subCatNameEng { get; set; }

    public SubCatagory(int Id, string NameHindi, string NameEng)
    {
        this.subCatagoryId = Id;
        this.subCatNameEng = NameEng;
        this.subCatNameHindi = NameHindi;
    }
}

when I am converting this list to json string by using Newtonsoft.Json it returns array of empty objects.

  string json=JsonConvert.SerializeObject(list);

I am getting following result.

> [{},{},{},{},{}]

Please help me regarding this problem.

asp.net Solutions


Solution 1 - asp.net

By default, NewtonSoft.Json will only serialize public members, so make your fields public:

public class Catagory
{
    public int catagoryId;
    public string catagoryNameHindi;
    public string catagoryNameEnglish;
    public List<Object> subCatagories;

    public Catagory(int Id, string NameHindi, string NameEng, List<Object> l)
    {
        this.catagoryId = Id;
        this.catagoryNameHindi = NameHindi;
        this.catagoryNameEnglish = NameEng;
        this.subCatagories = l;
    }
}

If for some reason you really don't want to make your fields public, you can instead decorate them with the JsonPropertyAttribute to allow them to be serialized and deserialized:

[JsonProperty]
int catagoryId;

This attribute also allows specifying other options, such as specifying the property name to use when serializing/deserializing:

[JsonProperty("categoryId")]
int Category;

Solution 2 - asp.net

You could also decorate your class to serialize all members you want without having to specify [JsonProperty] for each of them.

[JsonObject(MemberSerialization.OptOut)]
public class Catagory {
    ...
}

The MemberSerialization enum allows you to specify what members you want to serialize:

  • MemberSerialization.OptOut: All public members are serialized.
  • MemberSerialization.OptIn: Only members marked with JsonPropertyAttribute or DataMemberAttribute are serialized.
  • MemberSerialization.Fields: All public and private members are serialized.

Solution 3 - asp.net

Another cause of this problem--the class I was attempting to serialize derived from a base class that had the [DataContract] attribute, but the derived class lacked this attribute. Once I added [DataContract] to the derived class and [DataMember] to all of the public properties of the derived class it began working immediately.

Solution 4 - asp.net

A different problem in my case, It appears if you mark a class as [DataContract] then the properties to be serialized need to be marked [DataMember] and also should be Public.

In my case, I was migrating from WCF to web API , so didnt require any of DataContract or DataMember, so I removed all and it got serialised fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVivek MishraView Question on Stackoverflow
Solution 1 - asp.netJLRisheView Answer on Stackoverflow
Solution 2 - asp.netjoalcegoView Answer on Stackoverflow
Solution 3 - asp.netTom ReganView Answer on Stackoverflow
Solution 4 - asp.netManish BasantaniView Answer on Stackoverflow