0
我正在調用一個Web服務,該服務返回一個包含來自DataSet的多個表對象的JSON字符串。我試過無數的選項,並且無法訪問我的成功函數中的JSON數組。在ajax調用中,data.d返回一個字符串,data.d.Table返回undefined。 JSON字符串驗證。有人能告訴我我做錯了什麼嗎?我對JSON相當陌生。具有多個表的JSON字符串
JSON字符串:
{"d":"{\"Table\":[{\"PID_Key\":1379391,\"EMP_NAME\":\"Alemao, Francisco | 3024250\"},{\"PID_Key\":1405973,\"EMP_NAME\":\"De Michieli, Francis | 3024570\"},{\"PID_Key\":1143321,\"EMP_NAME\":\"Ferris, Nick | 3024340\"},{\"PID_Key\":1557903,\"EMP_NAME\":\"Frison, Monika | 3024938\"},{\"PID_Key\":1220009,\"EMP_NAME\":\"Hand, Niccoli | 3024358\"},{\"PID_Key\":1592556,\"EMP_NAME\":\"Kearns, Ronald | 3024183\"},{\"PID_Key\":1552113,\"EMP_NAME\":\"Kurcz, Krystal | 3024911\"},{\"PID_Key\":1637525,\"EMP_NAME\":\"Leasure, Katherine | 3024100\"},{\"PID_Key\":1637274,\"EMP_NAME\":\"Myers, Lynn | 3024106\"},{\"PID_Key\":1418148,\"EMP_NAME\":\"Olson, Amy | 3024663\"},{\"PID_Key\":1368903,\"EMP_NAME\":\"Rosemond, Courtniegh | 3024110\"},{\"PID_Key\":1637715,\"EMP_NAME\":\"Sims, Jeryka | 3024108\"},{\"PID_Key\":1637610,\"EMP_NAME\":\"Wilder, Ashley | 3024097\"}],\"Table1\":[{\"ExceptCode\":\"BRKHR\",\"Description\":\"BRKHR - Approved Paid HR Break\"},{\"ExceptCode\":\"COACH\",\"Description\":\"COACH - Coachings, 1 on 1s, and reviews\"},{\"ExceptCode\":\"MEET\",\"Description\":\"MEET - All Meetings\"},{\"ExceptCode\":\"MEETOFFS\",\"Description\":\"MEETOFFS - Meeting at Offsite Location\"},{\"ExceptCode\":\"PROJ\",\"Description\":\"PROJ - SME, POC, and all other projects\"},{\"ExceptCode\":\"PROJOFFS\",\"Description\":\"PROJOFFS - Project at Offsite Location\"},{\"ExceptCode\":\"PROTIME\",\"Description\":\"PROTIME - SavePro – For SavePro Huddles and Coaching Only\"},{\"ExceptCode\":\"SYSTEM\",\"Description\":\"SYSTEM - Systems issues\"},{\"ExceptCode\":\"TRAIN\",\"Description\":\"TRAIN - Training Exception (not CBT)\"},{\"ExceptCode\":\"TRAIN_CBT\",\"Description\":\"TRAIN_CBT - Computer Based Training\"},{\"ExceptCode\":\"TRAINOFFS\",\"Description\":\"TRAINOFFS - Train at Offsite Location\"}],\"Table2\":[{\"Dept\":\"1\"}]}"}
阿賈克斯:
$.ajax({
type: 'POST',
url: 'ExceptionServices.asmx/GetAgentsCodes',
contentType: "application/json; charset=utf-8",
data: '{"supPidKey":"' + supPidKey + '" }',
dataType: "json",
success: function (data) {
alert(data.d);
alert(data.d.Table);
$.each(data.d.Table, function (PID_Key, val) {
});
},
error: function (jqXHR, ajaxOptions, thrownError) {
alert(jqXHR.responseText);
}
});
C#:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetAgentsCodes(int supPidKey)
{
classes.ExceptionObject excObj = new classes.ExceptionObject();
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(JsonMethods.ToJson(excObj.getAgentsCodes(supPidKey)));
}
public static Dictionary<string, object> ToJson(DataSet data)
{
Dictionary<string, object> d = new Dictionary<string, object>();
foreach (DataTable table in data.Tables)
{
d.Add(table.TableName, RowsToDictionary(table));
}
return d;
}