我在使用C#中的对象数组序列化为JSON时遇到了一些麻烦。
我的代码:
friendlyNotification[] notifications = ns.friendlyNotifications.ToArray();
string json = JsonConvert.SerializeObject(notifications);
return Json(json, JsonRequestBehavior.AllowGet);
上面的代码返回以下内容:
[{}]
有没有人知道为什么这不起作用?与我试图序列化数组这一事实有关吗?
感谢。
答案 0 :(得分:2)
由于friendlyNotification
是没有公共属性的类,notifications
只包含一个元素,因此json结果为[{}]
。
json [{}]
具有带空对象的数组的含义。