简体   繁体   中英

JSON DATA formatting in WebAPI

public class CalendarController : ApiController
    {

        Events[] events = new Events[]  
        {  
            new Events { title= "event1", start = System.DateTime.UtcNow, end = System.DateTime.UtcNow },  
            new Events { title= "event2", start = System.DateTime.UtcNow, end = System.DateTime.UtcNow },  
            new Events { title= "event3", start = System.DateTime.UtcNow, end = System.DateTime.UtcNow}  
        };

        public IEnumerable<Events> GetAllCalendar()
        {
            return events;
        }

The JSON result for the above is

[{
    "title": "event1",
    "start": "2012-12-05T22:52:35.6471712Z",
    "end": "2012-12-05T22:52:35.6471712Z"
},{
    "title": "event2",
    "start": "2012-12-05T22:52:35.6471712Z",
    "end": "2012-12-05T22:52:35.6471712Z"
},{
    "title": "event3",
    "start": "2012-12-05T22:52:35.6471712Z",
    "end": "2012-12-05T22:52:35.6471712Z"
}]​
  1. How to create the same JSON result without the double quotes but single quote.
  2. How to get the date in the format of 'YYYY-MM-DD HH:MM:SS' Thank you, Smith
  1. As far as I know you can't. See http://json.org/
  2. Try System.DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss")! More info on Date and time formatting: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

By far the easiest way to deal with JSON in C# is to use the excellent JSON.net library:

http://james.newtonking.com/pages/json-net.aspx

Also, use the following excellent web site which takes your JSON and creates the relevant C# classes:

http://json2csharp.com/

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM