我正在调用一个Web服务,它返回一个包含来自DataSet的多个表对象的JSON字符串。我尝试了无数的选项,并且无法在我的成功函数中访问JSON数组。在ajax调用中,data.d返回一个字符串,data.d.Table返回undefined。 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:
$.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;
}
答案 0 :(得分:0)
如果其他人有类似问题......我回答了我自己的问题。在访问之前需要解析我的成功函数中返回的数据。
success: function (data) {
var parsed = $.parseJSON(data.d);
$.each(parsed.Table, function (i, val) {
alert(val.PID_Key);
});
},