Populate Calendar with JSON problems with converting unixtimestamp
Hi need some help with dateformat in json feed
I have a jquery calendar who i populate in a surfacecontroller.
json looks like this.
I need help with the date -> double timestamp = 1519551404;
public class CalendarController : SurfaceController
{
public JsonResult GetDiaryEvents()
{
// Get Data from Umbraco
var allevents = Umbraco.TypedContent(3283).Children();
var eventList = from l in allevents
select new
{
start = l.GetPropertyValue("eventStartDate"),
end = l.GetPropertyValue("eventEndDate"),
title = l.GetPropertyValue("header").ToString(),
content = l.GetPropertyValue("ingress").ToString()
};
var rows = eventList.ToArray();
return Json(rows, JsonRequestBehavior.AllowGet);
}
private static DateTime ConvertFromUnixTimestamp(double timestamp)
{
var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}}
An error is thrown at "content": "\some text;\u003c/p\u003e". If you add another backslash to the first backslash, the line reads "content": "\\some text;\u003c/p\u003e" and validates perfectly.
This is because in JSON, the backslash is a reserved character. In order to create backslashes, it should be replaced with \\.
You'll need to look more into the do's and don'ts for parsing data, but hopefully this points you in the right direction.
The AJAX post might differ depending on what jQuery calendar you're using.
There's a decent tutorial on Youtube here which uses Fullcalendar. If you skip about 20 minutes in, you'll see some AJAX code which might point you in the right direction.
It seems there's two separate issues/questions:
Resolving JSON format (which ties into your original question)
Rendering events in a jQuery Calendar view (separate question)
Once you're sure your JSON is valid (test it out here), I'd recommend posting a seperate question on the forum regarding rendering.
No problem! If you think any of the answers above answered your main question, make sure to mark it as solved so others can quickly find the solution :)
Populate Calendar with JSON problems with converting unixtimestamp
Hi need some help with dateformat in json feed I have a jquery calendar who i populate in a surfacecontroller. json looks like this. I need help with the date -> double timestamp = 1519551404;
[{"start":"\/Date(1560981600000)\/","end":"\/Date(1561154400000)\/","title":"header","content":"\some text;\u003c/p\u003e"},
Code in Surfacecontroller
In JSON, keys must be strings, written with double quotes (as stated here).
Why is it that you want to remove the double quotes?
Thanks I thought they caused errors in the calendar, my problem is the date I think or is it that content is empty? I got error in console
My feed
If you put the following line into a JSON validator:
An error is thrown at
"content": "\some text;\u003c/p\u003e"
. If you add another backslash to the first backslash, the line reads"content": "\\some text;\u003c/p\u003e"
and validates perfectly.This is because in JSON, the backslash is a reserved character. In order to create backslashes, it should be replaced with
\\
.You'll need to look more into the do's and don'ts for parsing data, but hopefully this points you in the right direction.
Hi Rhys can you help me with the ajax post? Many thanks
The AJAX post might differ depending on what jQuery calendar you're using.
There's a decent tutorial on Youtube here which uses Fullcalendar. If you skip about 20 minutes in, you'll see some AJAX code which might point you in the right direction.
It seems there's two separate issues/questions:
Once you're sure your JSON is valid (test it out here), I'd recommend posting a seperate question on the forum regarding rendering.
Many thanks for your help
No problem! If you think any of the answers above answered your main question, make sure to mark it as solved so others can quickly find the solution :)
is working on a reply...