67

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.

5
  • 1
    Are you sure the list is not the list of the null Objects. As you have declared Parameterzied constructor. If you want to create empty object then you have to manually declare the empty constructor in class. Commented Mar 12, 2015 at 6:38
  • yeah there are no null objects in the list Commented Mar 12, 2015 at 6:39
  • 2
    I was having the same problem using the Jackson library. Making the fields public solved the problem there too. Commented Dec 6, 2016 at 15:17
  • 1
    Not having public properties was exactly my issue as well. Commented Jan 21, 2020 at 23:22
  • 1
    Properties marked internal are a similar issue. See this post for ideas: stackoverflow.com/questions/26873755/… Commented Sep 29, 2020 at 13:59

4 Answers 4

145

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;
Sign up to request clarification or add additional context in comments.

7 Comments

Public members of the class Is not the best solution. Does Exist other better solution?
@IgnacioChiazzo Yes, you should be able to use the JsonPropertyAttribute to indicate that a non-public field or property should be serialized: [JsonProperty]private int categoryId;
@JLRishe - You should add your comment as an answer as this is now how I do it rather than making everything public
@ChrisHammond Updated my answer.
pulling my hair out for an hour wondering why this didn't work for one class but did for another; overlooked that I did not declare my properties as public!
|
19

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.

2 Comments

it really saved my day
This solution just saved me. I have never had to do this before, and have no idea why I suddenly need to do it now... but I won't forget it.
15

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.

1 Comment

A similar thing seemed to happen to me, except with an inner class (outer had [DataContract], but inner did not). The same solution fixed it.
10

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.