Newtonsoft.Json serialization returns empty json object

后端 未结 4 1430
轻奢々
轻奢々 2020-12-25 10:09

I have list of objects of following class:

public class Catagory
{
    int catagoryId;
    string catagoryNameHindi;
    string catagoryNameEnglish;
    List         


        
4条回答
  •  醉话见心
    2020-12-25 10:29

    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 subCatagories;
    
        public Catagory(int Id, string NameHindi, string NameEng, List l)
        {
            this.catagoryId = Id;
            this.catagoryNameHindi = NameHindi;
            this.catagoryNameEnglish = NameEng;
            this.subCatagories = l;
        }
    }
    
    
    

    Edit: 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;
    

    提交回复
    热议问题