2016-10-03 42 views
-2

了Methode在Web服務jsonArray未示出(web服務)

了Methode:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] 
public void GetList() 
{ 
     JavaScriptSerializer ser = new JavaScriptSerializer(); 

     Test[] test=new Test[3]; 
     test[0] = new Test("1", "anex"); 
     test[1] = new Test("2", "Ahmed"); 
     test[2] = new Test("3", "Karim"); 

     var json = new 
     { 
      list=test , 
      DateTime=DateTime.Now.ToShortDateString()        
     }; 

     HttpContext.Current.Response.Write(ser.Serialize(json)); 
} 

結果:{"list":[{},{},{}],"DateTime":"03/10/2016"}

列表不顯示!!

+1

請添加'測試'類的定義。如果它沒有屬性或只有私有屬性,那麼串行器將忽略它們。 –

+0

public class Test { private string id {get;組; } 私人字符串名稱{get;組; } public Test(){} public Test(String i,String n) { this.id = i; this.name = n; } } –

+0

謝謝@rboe它的工作就像一個魅力 –

回答

0

Test類只包含專用字段。因此JavaScriptSerializer會忽略它們。將它們聲明爲public,它們將顯示在生成的JSON中。

+0

謝謝它的工作 –

+0

@ Ane'xBm - 不客氣。如果回答您的問題,請不要忘記標記帖子爲答案。 –