Newtonsoft.Json serialization returns empty json object
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.
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;