tags:

views:

46

answers:

2

I am working with the FullCalendar module (great, btw) and I'm having trouble populating events from the database. I am receiving a JSON string created by a C# web service. However, when I try to parse it and print out test alerts, I get nothing but "undefined".

My response string looks like this in Firebug:

{d="[{"ScheduleRecId":9,"EmployeeScheduled":"3","TimeStart":"\/Date(1285601677000)\/","TimeEnd":"\/Date(1285601677000)\/","UpdatedBy":"4","LastUpdate":"\/Date(1285601677000)\/","Started":true,"Finished":false}]"}

which appears to be an array, but when I try to access it from JQuery like:

 success: function(doc) {
              alert(doc)    //echos "Object oject"
              alert(doc[0]) //echos "undefined"
              alert(doc.EmployeeScheduled) //echos "null"
 }

I also tried using JSON.parse and eval() with not much luck. How can I access the properties of this object?

UpDate: After Nick's answer, I decided to try alert(doc.d[0]); which echoed [

I noticed that if I tried alert(doc.d[5]); I get h leading me to believe doc.d is coming across as a character array. I suppose I could read it in and parse it, but shouldn't there be a cleaner way to access the properties?

+1  A: 

Since it's from ASP.NET 3.5 or higher it looks like, you need the d property, like this:

success: function(doc) {
    alert(doc.d[0].EmployeeScheduled);
}

This is because the root object has one property, d, which is an array of objects...so use [0] to get the first entry, then .EmployeeScheduled to get that property.

Nick Craver
@Nick Craver - I got "undefined" with this. I double checked and found in a different place in Firebug showing the entire string enclosed in {} (I updated my post) - would this change what the alert should look like?
cinqoTimo
@cinqoTimo -Are you looking at the JSON tab or the Net tab to see the raw response?
Nick Craver
A: 

What ended up working was this:

 success: function(obj) {

      var schedules = (eval(obj.d));

then I could capture the properties like this:

var event = {
                id: schedules[0].ScheduleRecId,
            }
cinqoTimo
I usually change my code written with .net 2 as followingsuccess: function(obj) {var doc = obj.d;then you can use your doc as usual
FrenchiInLa
Thanks, that's a little easier to understand and maintain. I will make that change.
cinqoTimo