SkyeBoniwell
SkyeBoniwell

Reputation: 7092

Unknown issue with JSON string formatting

I'm passing data to my implementation of the FullCalendar via JSON.

My feed looks ok, but for some reason the calendar doesn't display any data.

Here is an example of my JSON feed:

[{
  "title":"Resourse for Paths ",
  "start":"2011-11-08T07:30:00Z",
  "end":"2011-11-08T10:30:00Z",
  "allDay":false
}]

According to the FullCalendar manual, everything looks ok, but I am getting no data at all.

Does anyone see anything that I don't?

Here is the code that generates the feed:

 Private Function makejsonoftable(ByVal table As DataTable, ByVal e As makejson) As String
    Dim sb As New StringBuilder()
    Dim cultureName As String = "en-US"
    Dim culture As CultureInfo = New CultureInfo(cultureName)
    Dim fieldvalue As String
    For Each dr As DataRow In table.Rows
        If sb.Length <> 0 Then
            sb.Append(",")
        End If
        sb.Append("{")
        Dim sb2 As New StringBuilder()
        For Each col As DataColumn In table.Columns
            Dim fieldname As String = col.ColumnName

            If TypeOf (dr(fieldname)) Is Boolean Then
                fieldvalue = Convert.ToBoolean(dr(fieldname))
            Else
                If TypeOf (dr(fieldname)) Is DateTime Then
                    fieldvalue = Convert.ToDateTime(dr(fieldname))
                Else
                    fieldvalue = dr(fieldname).ToString()
                End If
            End If

            If sb2.Length <> 0 Then
                sb2.Append(",")
            End If
            If TypeOf (dr(fieldname)) Is Boolean Then
                sb2.Append(String.Format("""{0}"":{1}", fieldname, fieldvalue.ToLower))
            Else
                If TypeOf (dr(fieldname)) Is DateTime Then
                    sb2.Append(String.Format("""{0}"":""{1:ddd, dd MMM yyyy HH':'mm':'ss 'EST'}""", fieldname, Convert.ToDateTime(fieldvalue)))
                Else
                    sb2.Append(String.Format("""{0}"":""{1}""", fieldname, fieldvalue))
                End If
            End If


        Next
        sb.Append(sb2.ToString())

        sb.Append("}")
    Next
    If e = makejson.e_with_square_brackets Then
        sb.Insert(0, "[")
        sb.Append("]")
    End If
    Return sb.ToString()


End Function

Public Enum makejson
    e_without_square_brackets
    e_with_square_brackets
End Enum

Thanks

Upvotes: 0

Views: 703

Answers (3)

Blll P
Blll P

Reputation: 118

My JSON feed for FullCalendar is written in PHP. I get the event info from my MySQL database and format it in PHP, and output it as a JSON file for the calendar. This is part of the code:

$jsonfeed = "[
";
while($row = mysql_fetch_array($result))
{
// some formatting happens here, then:
  $jsonfeed .= "
    {
    \"title\"   : \"" . $this_group . "\",
     \"start\"   : \"" . $row[formatDate] . " " . $row[formatStart] . "\",
     \"end\"     : \"" . $row[formatDate] . " " . $row[formatFinish] . "\",
     \"description\"    : \"" . $description . "\",
     \"url\" : \"http://mysite.com/" . $row[id] . "\",
     \"allDay\"  : false
   },";
}
// remove the final comma, add final ]
$jsonfeed = substr($jsonfeed, 0, -1) . "
]
";

This is what ended up working for me, after quite a number of variations. Apparently, I needed to have those line breaks (it just wouldn't work without them). So, that's something you might want to try. Also, I'm using a local time zone, so I didn't need the final "Z" and I have a space instead of the "T" between date and time. I don't know if that will affect your code, but it's something else to try.

Also, have you tried putting your JSON code through a validator, to check it?

Upvotes: 1

Juan Gonzales
Juan Gonzales

Reputation: 1977

I think the problem is the quotes around the variables (aka: title, start, end, and allday). Remove the quotes and see what happens.

EDIT: An example from docs

$('#calendar').fullCalendar({

    eventSources: [

        // your event source
        {
            url: '/myfeed.php', // use the `url` property
            color: 'yellow',    // an option!
            textColor: 'black'  // an option!
        }

        // any other sources...

    ]

});

Upvotes: 1

blankabout
blankabout

Reputation: 2627

Just the other day I was doing some debugging and introduced some linefeeds/carriage returns into some JSON which was used in an AJAX response. Safari did not mind but Firefox did not like it because linefeeds and carriage returns are not valid JSON, so that could well be your problem.

Upvotes: 1

Related Questions