我有一个返回 JSON 对象的 web 服务,但是当我尝试遍历它时,每个对象中的每个项目都返回 undefined
这是从 web 服务返回的 JSON 对象
[{"result":14,"question":"6 - 7 مرات اسبوعيا","aID":70},{"result":29,"question":"3 - 5 مرات اسبوعيا","aID":71},{"result":41,"question":"مرة واحدة اسبوعيا","aID":72},{"result":14,"question":"1 - 3 مرات شهريا","aID":73}]
以及我如何遍历它:
var resultAsJson = data.d;
$.each(resultAsJson, function (index, resObject) {
$('#pollResults').append('<p><strong>' + resObject.result + ' ' +
resObject.question + '</strong></p>');
alert(resObject.question);
});
- - - - - - - - - 更新 - - - - - - - - -
嗨,伙计们,上面的代码运行良好,问题是我从 web 服务返回的 JSON 响应被序列化如下:
Dim m_result As New Data.Objects.ObjectParameter("Result", GetType(Boolean))
Dim lstofresult As List(Of addPollvote_Result) = Context.addPollvote(para_pid, para_aid, Date.Now, m_UID, Nothing, HttpContext.Current.Request.ServerVariables("REMOTE_ADDR"), Nothing, m_result).ToList
Dim m_json As New Script.Serialization.JavaScriptSerializer
Return m_json.Serialize(lstofresult)
当我删除序列化并返回列表时,它运行良好,请参见下面的工作代码。
Dim m_result As New Data.Objects.ObjectParameter("Result", GetType(Boolean))
Dim lstofresult As List(Of addPollvote_Result) = Context.addPollvote(para_pid, para_aid, Date.Now, m_UID, Nothing, HttpContext.Current.Request.ServerVariables("REMOTE_ADDR"), Nothing, m_result).ToList
Return lstofresult
它工作得很好。