2) the JsonConvert.Serialize looks like it issues the $id field format. I'd like to change the format to # instead of $id but dont know where to access this formatting.
3) The "Date" field (a Date datatype from entityframework) isnt showing the proper date format. E.g. "Date\":\"\\/Date(1314198402793-0400). I need to get the date in the format of: \"Date\":\"2011-08-24 11:06:42\
1.When I did a simple sample using JsonConvert.SerializeObject on my computer, but I cannot reproduce your issue based on my code, you might refer to the following code:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
protected void Page_Load(object sender, EventArgs e)
{
List<Employee> eList = new List<Employee>();
Employee el = new Employee();
el.Name = "Minal";
el.Age = 24;
el.LogDate = new DateTime(2009, 2, 15, 11, 06, 24, DateTimeKind.Utc);
eList.Add(el);
el = new Employee();
el.Name = "Santosh";
el.Age = 24;
el.LogDate = new DateTime(2012, 2, 20, 17, 08, 08, DateTimeKind.Utc);
eList.Add(el);
string ans = JsonConvert.SerializeObject(eList,Formatting.None,new IsoDateTimeConverter());
}
public class Employee
{
public string Name;
public int Age;
public DateTime LogDate;
}
and at the begin of JSON string doesn't has the method name, it sees as follows:
2.If you want to replace the char '$' as '#' in the JSON string, you can parse the JSON string and use string.replace method to do it.
3.Use one of the JsonConverters that come with Json.NET for working with dates to get a better format. JavaScriptDateTimeConverter will automatically give you a JavaScript date, see above the code I give for you.
ljp007
Member
663 Points
177 Posts
WebGet response formatting questions
Feb 08, 2012 11:47 PM|LINK
I'm running this code:
[WebGet]
public string MyActivity(string total_records, string results, string start_index, string sort, string dir, string sql_params)
{
///some logic here
return JsonConvert.SerializeObject(activity, Formatting.None);
}
which returns this response:
{ "d" : { "MyActivity": "{\"records_returned\":\"1\",\"total_records\":\"143\",\"start_index\":\"0\",\"sort\":\"Date\",\"filter\":null,\"dir\":\"4/14/2011 7:26:21 PM\", \"records\":[{\"$id\":\"1\",\"Activity\":\"viewed\"}],\"error_msg\":\"\",\"meta\":[{\"key\":\"$id\",\"label\":\"$id\",\"type\":\"string\",\"sortable\":\"false\"}]}" } }
I need to make 3 updates.
1) remove the Methodname from the response (the outer {} wrapping), so the format looks like this:
{\"records_returned\":\"1\",\"total_records\":\"143\",\"start_index\":\"0\",\"sort\":\"Date\",\"filter\":null,\"dir\":\"4/14/2011 7:26:21 PM\" , \"records\":[{\"$id\":\"1\",\"Activity\":\"viewed\"}],\"error_msg\":\"\",\"meta\":[{\"key\":\"$id\",\"label\":\"$id\",\"type\":\"string\",\"sortable\":\"false\"}]}
2) the JsonConvert.Serialize looks like it issues the $id field format. I'd like to change the format to # instead of $id but dont know where to access this formatting.
3) The "Date" field (a Date datatype from entityframework) isnt showing the proper date format. E.g. "Date\":\"\\/Date(1314198402793-0400). I need to get the date in the format of: \"Date\":\"2011-08-24 11:06:42\
Thanks for any help
Peter pi - M...
Star
12871 Points
1786 Posts
Re: WebGet response formatting questions
Feb 13, 2012 01:54 AM|LINK
Hi,
1.When I did a simple sample using JsonConvert.SerializeObject on my computer, but I cannot reproduce your issue based on my code, you might refer to the following code:
using Newtonsoft.Json; using Newtonsoft.Json.Converters; protected void Page_Load(object sender, EventArgs e) { List<Employee> eList = new List<Employee>(); Employee el = new Employee(); el.Name = "Minal"; el.Age = 24; el.LogDate = new DateTime(2009, 2, 15, 11, 06, 24, DateTimeKind.Utc); eList.Add(el); el = new Employee(); el.Name = "Santosh"; el.Age = 24; el.LogDate = new DateTime(2012, 2, 20, 17, 08, 08, DateTimeKind.Utc); eList.Add(el); string ans = JsonConvert.SerializeObject(eList,Formatting.None,new IsoDateTimeConverter()); } public class Employee { public string Name; public int Age; public DateTime LogDate; }and at the begin of JSON string doesn't has the method name, it sees as follows:
[{"Name":"Minal","Age":24,"LogDate":"2009-02-15T11:06:24Z"},{"Name":"Santosh","Age":24,"LogDate":"2012-02-20T17:08:08Z"}]2.If you want to replace the char '$' as '#' in the JSON string, you can parse the JSON string and use string.replace method to do it.
3.Use one of the JsonConverters that come with Json.NET for working with dates to get a better format. JavaScriptDateTimeConverter will automatically give you a JavaScript date, see above the code I give for you.
Hope this hopes
Regards,
Peter
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework