I have the following Object:
[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
}
I have aa Class which has a variable:
private List<Project> projects
The corresponding get method is
public List<Project> getUserProjects()
{
return projects;
}
I'm trying to convert this List to a JSON format to use with an AJAX service but the result is always [{},{}] (The user has 2 projects - if 3 than the result is [{},{},{}]
This is how I serialize the list of objects using JSON.NET:
[OperationContract]
public string GetProjects()
{
string json = JsonConvert.SerializeObject(user.getUserProjects());
return json;
}
Should I iterate through the list first or...? Any help will be greatly appreciated!
Thanks!
EDIT:
I would like the JSON output to be like:
{
"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
]
}
Could you use the JavaScriptSerializer for this (System.Web.Script.Serialization). Try:
JavaScriptSerializer serializer = New JavaScriptSerializer();
String json = serializer.Serialize(listobject);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.