我有以下对象:
[Serializable]
public class Project
{
private int projectId;
private int userId;
private String projectName;
private String projectDescription;
private DateTime startDate;
private DateTime endDate;
...constructor + get/set methods removed
}
我有一个类,它有一个变量:
private List<Project> projects
对应的获取方法是
public List<Project> getUserProjects()
{
return projects;
}
我正试图将此列表转换为JSON格式,以便与AJAX服务一起使用,但结果总是[{},{}]
这就是我使用JSON.NET:序列化对象列表的方式
[OperationContract]
public string GetProjects()
{
string json = JsonConvert.SerializeObject(user.getUserProjects());
return json;
}
我应该先遍历列表还是。。。?任何帮助都将不胜感激!
谢谢!
编辑:
我希望JSON输出如下:
{
"Projects": [
{ "ProjectID":"theID" , "userID":"theuserID" , "ProjectName":"theName" , "ProjectDescription":"theDescription" , "startDate":"thedate" , "endDate":"theDate" },
{ "ProjectID":"theID" , "userID":"theuserID" , "ProjectName":"theName" , "ProjectDescription":"theDescription" , "startDate":"thedate" , "endDate":"theDate" },
...etc
]
}
你能为此使用JavaScriptSerializer吗(System.Web.Script.Serialization)。试试:
JavaScriptSerializer serializer = New JavaScriptSerializer();
String json = serializer.Serialize(listobject);