使用JQuery从.NET Web服务获取JSON

时间:2022-10-24 12:41:14

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".

我正在使用FullCalendar模块(很棒,顺便说一句),我在填充数据库中的事件时遇到了麻烦。我收到一个由C#Web服务创建的JSON字符串。但是,当我尝试解析它并打印出测试警报时,除了“未定义”之外我什么也得不到。

My response string looks like this in Firebug:

我的响应字符串在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:

这似乎是一个数组,但当我尝试从JQuery访问它时,如:

 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?

我也试过用JSON.parse和eval()运气不好。如何访问此对象的属性?

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

UpDate:在Nick的回答之后,我决定尝试提醒(doc.d [0]);与之相呼应[

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?

我注意到,如果我尝试过警报(doc.d [5]);我得告诉我相信doc.d作为一个字符数组出现了。我想我可以阅读并解析它,但是不应该有更清晰的方式来访问属性?

2 个解决方案

#1


1  

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

因为它来自ASP.NET 3.5或更高版本,所以你需要d属性,如下所示:

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.

这是因为根对象有一个属性d,它是一个对象数组...所以使用[0]获取第一个条目,然后使用.EmployeeScheduled获取该属性。

#2


0  

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

#1


1  

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

因为它来自ASP.NET 3.5或更高版本,所以你需要d属性,如下所示:

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.

这是因为根对象有一个属性d,它是一个对象数组...所以使用[0]获取第一个条目,然后使用.EmployeeScheduled获取该属性。

#2


0  

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