1

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"}]
Laurel
  • 5,965
  • 14
  • 31
  • 57
IKid
  • 43
  • 7

2 Answers2

1

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/

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • Thanks for the response! So to have it be with my API URL, where would I add that? – IKid Apr 12 '16 at 17:34
  • Sorry..not understanding what do you want to achieve. – Debug Diva Apr 12 '16 at 17:39
  • Sorry for not being clear. We want to format our API JSON, which is the second block of code I included in the post, like the first block of code included in the post. We are trying to get Full Calendar to GET our events from an API, and it won't work the way we have the JSON formatted. The first block of code, does work. We're trying to make ours work. Thanks! @R J – IKid Apr 12 '16 at 17:44
  • In my original post above, there are two blocks of code, this is the second = [{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}] – IKid Apr 12 '16 at 17:47
  • So, in my answer we are getting the JSON in same way you want as in your second block. – Debug Diva Apr 12 '16 at 17:48
  • Yes, but is there a way to GET it from the URL, so if the API content changes or more is added to it, we don't have to add it by manually coding it in. – IKid Apr 12 '16 at 17:49
0

Talking about your API you are saying that '(everything is a string)'.

To convert string into JSON use JSON.parse() docs

var validJSON =  JSON.parse('[{"start":"2016-04-12","end":"2016-04-12","title":"Calendar 1","id":"a41380d1fbbaa819"}]');

Will return:

[{
    "start": "2016-04-12",
    "end": "2016-04-12",
    "title": "Calendar 1",
    "id": "a41380d1fbbaa819"
}]
oKonyk
  • 1,468
  • 11
  • 16
  • Thanks for the response! So for something like this, https://jsbin.com/nalefi/edit?js,output How would I implement my URL to be used, not the events coded in, with what you're talking about? – IKid Apr 12 '16 at 17:28