嘗試添加[ScriptMethod(ResponseFormat = ResponseFormat.Json)]像這樣將WebMethod下:
[WebService(Namespace="my.soap")]
public class StockQuote : WebService
{
[WebMethod(Description="",EnableSession=false)]
public ResultSet IBM()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = "{\"ResultSet\":{\"Query\":\"ibm\",\"Result\":[{\"symbol\":\"IBM\",\"name\": \"International Business Machines Corp.\",\"exch\": \"NYQ\",\"type\": \"S\",\"exchDisp\":\"NYSE\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.F\",\"name\": \"IBM\",\"exch\": \"FRA\",\"type\": \"S\",\"exchDisp\":\"Frankfurt\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.DE\",\"name\": \"IBM\",\"exch\": \"GER\",\"type\": \"S\",\"exchDisp\":\"XETRA\",\"typeDisp\":\"Equity\"},{\"symbol\":\"^AXI\",\"name\": \"Stlmt ID - NASDAQ OMX Alpha IBM\",\"exch\": \"NAS\",\"type\": \"I\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Index\"},{\"symbol\":\"^IVSPY\",\"name\": \"NASDAQ OMX Alpha IBM vs. SPY\",\"exch\": \"NAS\",\"type\": \"I\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Index\"},{\"symbol\":\"IBMSX\",\"name\": \"Invesco Multi-Sector B\",\"exch\": \"NAS\",\"type\": \"M\",\"exchDisp\":\"NASDAQ\",\"typeDisp\":\"Fund\"},{\"symbol\":\"IBM.BE\",\"name\": \"IBM\",\"exch\": \"BER\",\"type\": \"S\",\"exchDisp\":\"Berlin\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.SG\",\"name\": \"IBM\",\"exch\": \"STU\",\"type\": \"S\",\"exchDisp\":\"Stuttgart\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.BA\",\"name\": \"International Business Machines Corp.\",\"exch\": \"BUE\",\"type\": \"S\",\"exchDisp\":\"Buenos Aires\",\"typeDisp\":\"Equity\"},{\"symbol\":\"IBM.L\",\"name\": \"International Business Machines Corp.\",\"exch\": \"LSE\",\"type\": \"S\",\"exchDisp\":\"London\",\"typeDisp\":\"Equity\"}]}}";
return serializer.Deserialize<ResultSet>(json);
}
}
[Serializable]
public class ResultSet
{
public string Query;
public ResSet[] Result;
}
[Serializable]
public class ResSet
{
public string symbol;
public string name;
public string exch;
public string type;
public string exchDisp;
public string typeDisp;
}
我發現了以下通過Web服務的格式的對象,而不是返回
[WebMethod(Description="",EnableSession=false)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
您是否嘗試過使用調試器來查看您的JSON URL格式是否正確,或者查看正在返回的實際JSON數據。我使用[Web Development Helper](http://projects.nikhilk.net/WebDevHelper),但還有其他幾個。 – WEFX 2011-05-09 17:53:37
我必須檢查一下,看看它是否有幫助。我以前曾經查找過一些調試工具,但沒有一個選項可以直接使用。我一直在使用記事本來創建這個Web服務,這限制了我的調試選項。我應該看看我是否可以轉移到Visual Web Developer Express。 – Doug 2011-05-09 18:00:20
@ user745588:格式正確,我使用在線[JSON驗證器](http://jsonformatter.curiousconcept.com/)進行了檢查。問題是你的類不包含名爲'PropertySet'的**屬性**(參見[答案](http://stackoverflow.com/questions/5940528/c-asp-net-web-service-trying -to-deserialize-json-and-getting-an-empty-object/5940709#5940709))。 – Groo 2011-05-09 18:22:31