开发者

Getting JSON from a .NET web service with JQuery

开发者 https://www.devze.com 2023-01-18 07:36 出处:网络
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 par

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?


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.


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,
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号