【发布时间】:2015-05-14 05:20:04
【问题描述】:
我有以下类的对象列表:
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;
}
}
当我使用 Newtonsoft.Json 将此列表转换为 json 字符串时,它会返回空对象数组。
string json=JsonConvert.SerializeObject(list);
我得到以下结果。
[{},{},{},{},{}]
请帮我解决这个问题。
【问题讨论】:
-
您确定该列表不是空对象的列表吗?正如您已声明
Parameterzied构造函数。如果要创建空对象,则必须在类中手动声明empty构造函数。 -
是的,列表中没有空对象
-
我在使用 Jackson 库时遇到了同样的问题。公开这些字段也解决了那里的问题。
-
没有公共属性也是我的问题。
-
标记为内部的属性是一个类似的问题。见这篇文章的想法:stackoverflow.com/questions/26873755/…