[英]Serialize public properties using JSON.NET
I'm using JSON.NET implementation to serialize/deserialize .NET objects to JS and vice versa, all works fine until running GetWCFData()
in the following: 我正在使用JSON.NET实现将.NET对象序列化/反序列化为JS,反之亦然,直到在以下情况下运行
GetWCFData()
为止,所有方法都可以正常工作:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class WebLookup
{
WCFService.WCFServiceClient svc;
IsoDateTimeConverter DateConverter = new IsoDateTimeConverter();
List<WCFContract.Language> Languages { get; set; }
List<WCFContract.Group> Groups { get; set; }
List<WCFContract.User> Users { get; set; }
public WebLookup()
{
DateConverter.DateTimeFormat = "dd/MM/yyyy";
DateConverter.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeLocal;
}
public string GetWCFData()
{
svc = new WCFService.WCFServiceClient();
WebLookup weblookup = new WebLookup();
weblookup.Languages = svc.GetWCFLanguages().ToList();
weblookup.Groups = svc.GetWCFGroups().ToList();
weblookup.Users = svc.GetWCFUsers().ToList();
return JsonConvert.SerializeObject(weblookup, DateConverter);
}
}
Members Languages
, Groups
, and Users
get populated successfully when calling the WCF service but JsonConvert.SerializeObject(lookup, DateConverter)
always returns an empty JSON string to the client (web browser), this is strange as it usually works fine for me in other areas, the only difference is that here I have the populated WebLookup
members declared as public properties in the class itself. 调用WCF服务时,成员
Languages
, Groups
和Users
成功填充,但JsonConvert.SerializeObject(lookup, DateConverter)
始终向客户端(Web浏览器)返回一个空的JSON字符串,这很奇怪,因为通常在其他情况下我也可以正常使用方面,唯一的区别是在这里,我已将填充的WebLookup
成员声明为类本身中的公共属性。
必须将Languages,Groups和Users属性声明为Public成员,才能通过JSON.NET或内置的JavaScriptSerializer进行序列化,但在您的代码中并非如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.