2014-02-24 32 views
0

我想在$ .ajax中检索我的对象值。

我的web服务方法Statistic_1返回一个Object1,它有一个object2数组。

在C#代码中,我以这种方式检索:Object1.Items [0] .Name例如。

项目是我Object1的一个属性,它是object2的一个数组。

这里是我的JavaScript代码:

function getStatistic1() { 

    var response; 
    var allstat1 = []; 

    $.ajax({ 
    type: 'GET', 
    url: 'http://localhost:52768/Service1/Statistic_1', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function (msg) { 
      response = msg.d; 
      for (var i = 0; i < response.length; i++) { 

        allstat1[i] =**???** 

      } 

      fillData(allstat1); 

    }, 
    error: function (e) { 
    alert("error loading statistic 1"); 
    } 
    }) 
} 

function fillData(data) { 

    $('#table_campaigns').dataTable({ 
     **???** 
    }); 
} 

如何实现检索值?

编辑: @RoyiNamir,@汤姆Cammann
这里是我的代码在Service.cs

[OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 
    ResponseStatistic_1 Statistic_1(); 

ResponseStatistic_1类

public class ResponseStatistic_1 : IBaseClientEntity 
{ 
    public ResponseStatistic_1() 
    { 

    } 

    public ResponseStatistic_1(Statistic_1 [] items) : this() 
    { 
     this.Items = items; 
    } 

    #region Properties 
    public Statistic_1[] Items 
    { 
     get; 
     set; 
    } 

而且Statistic_1是类

public class Statistic_1 
{ 
    private string _geografisch_zone; 
    private decimal[] _sum; 
    private int _yearStart; 
    private int _yearEnd; 

      ... 
} 

我如何实现for循环?

+0

请问你的JSON样子? –

+0

响应对象是什么样的? –

+0

[如何通过使用jQuery读取多级Json数据](http://stackoverflow.com/questions/456887/how-to-read-multi-level-json-data-by-using-jquery ) – davethecoder

回答

0

根据您的响应对象...

如果它是一个数组,你正在返回:

function successFunction(data) { 
    for (var i = 0; i < data.length; i++) { 
     $('#table_campaigns').dataTable().fnAddData(data[i]) 
    }  
} 

你的Ajax调用:

$.ajax({ 
    type: 'GET', 
    url: 'http://localhost:52768/Service1/Statistic_1', 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: successFunction, 
    error: function (e) { 
    alert("error loading statistic 1"); 
    } 
    }) 
} 
+0

我不明白,因为我不认为这是我回来的数组。这是一个具有其他对象数组的对象。我把代码放在答案中。 – Jayce

+0

我试过这个:response = msg.Items。但是回应为空,为什么?但是,在我的WCFTestClient.exe中,Statistic_1方法运行良好。 – Jayce