Search code examples
javascriptjsonfullcalendardeployd

Error - Our JSON is formatted incorrectly. Events won't show for Full Calendar


How can we format our API Json like the first block of code below? Do we need to put it in an array, string, object? Our issue is that Full Calendar can get events from the first block of code, but not ours, the second block of code.

http://www.json-generator.com/api/json/get/ccUKVDYErS?indent=2

[
  {
"start": "2014-08-01", 
"title": "All Day Event"
 }, 
{
"start": "2014-01-07", 
"end": "2014-08-10", 
"title": "Long Event"
}, 
{
"start": "2014-08-09T16:00:00", 
"id": 999, 
"title": "Repeating Event"
}, 
{
"start": "2014-08-16T16:00:00", 
"id": 999, 
"title": "Repeating Event"
}, 
{
"start": "2014-08-12T10:30:00", 
"end": "2014-08-12T12:30:00", 
"title": "Meeting"
 }, 
 {
"start": "2014-08-22T12:00:00", 
"title": "Lunch"
}, 
{
"start": "2014-08-13T07:00:00", 
"title": "Birthday Party"
}, 
{
"url": "http://google.com/", 
"start": "2014-08-28", 
"title": "Click for Google"
}
 ]

Our API currently looks like this (everything is a string),

[{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}]

Solution

  • Try this it will work :

    As per your question.

    //JSON Text
    var obj = '[{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}]';
    
    //Convert JSON String in JSON Object
    console.log(JSON.parse(obj));
    

    Output :

    enter image description here

    As per your requirement :

    You have to pass these values in the url and making AJAX call to get the JSON according to these values.

    var start='2016-04-12';
    var end='2016-04-12';
    var title='Calendar 1';
    var id='var start='2016-04-12';';
    $.ajax({
    type: "POST",
    dataType: "json",
    url: "data.php", //Relative or absolute path to chat.php file
    data: 'start='+start+'&end='+end+'&title='+title+'&id='+id,
    success: function(data) {
    alert(data); // JSON From the URL
    }
    });
    return false;
    }
    

    Now you have to use server side language(PHP,Java) to create the JSON based on these values.

    Working Fiddle : https://jsfiddle.net/pr2daL7r/