我正在创建一个网页,以使用 ajax、vb 和 .net 使用 High Charts 动态创建图形和图表。我目前可以调用运行并返回 json 的 web 服务,但是我无法获得正确的 json。我用来返回 json 的函数可以对一个对象进行很好的序列化,但是我设置它的方式不止一个不正确。
<OperationContract()>
<WebMethod()>
Public Function DoWork() As String
' Add your operation implementation here
Dim prod As New product
Dim strConnString As String = ConfigurationManager.ConnectionStrings("STEMConnectionString").ConnectionString
Dim myConnection As SqlConnection
myConnection = New SqlConnection(strConnString)
myConnection.Open()
Dim strData As String = "SELECT Student_Survey_Response_Fact.StudentID, Student_Survey_Response_Fact.SurveyID, " & _
"Survey_ItemR.SurveyQuestionText, Student_Survey_Response_Fact.ResponseText, Survey_ItemR.SurveyResponseNo, Student_Lite.Gender " & _
"FROM Student_Survey_Response_Fact INNER JOIN " & _
"Survey_ItemR ON Student_Survey_Response_Fact.ResponseNo = Survey_ItemR.SurveyResponseNo INNER JOIN " & _
"Student_Lite ON Student_Survey_Response_Fact.StudentID = Student_Lite.StudentId INNER JOIN " & _
"Survey_Item ON Student_Lite.SchoolYear = Survey_Item.SchoolYear " & _
"WHERE (Student_Survey_Response_Fact.ResponseText <> 'NULL')"
Dim Command As New SqlCommand(strData, myConnection)
Dim reader As SqlDataReader
reader = Command.ExecuteReader
Dim stream As New MemoryStream
Dim jsonSerializer As New DataContractJsonSerializer(GetType(product))
While reader.Read
prod.stuID = reader("StudentID").ToString
prod.gender = reader("Gender").ToString
prod.surveyID = reader("SurveyID").ToString
prod.surveyQ = reader("SurveyQuestionText").ToString
prod.surveyR = reader("ResponseText").ToString
prod.surveyNum = reader("SurveyResponseNo").ToString
jsonSerializer.WriteObject(stream, prod)
End While
stream.Position = 0
Dim streamRead As New StreamReader(stream)
Return streamRead.ReadToEnd()
我知道它为什么会这样,但我不知道如何解决它。我把它放在jsonSerializer.WriteObject(stream, prod)
循环中,所以它为每个对象创建一个 json 字符串。问题是客户端的输出如下所示:
d:{"gender":"M","stuID":"005716305","surveyA":null,"surveyID":"201202","surveyNum":"Resp 09","surveyQ":"Please indicate how you feel about the following statement: - I would like to enter a science competition or science fair in the future.","surveyR":"Disagree"}{"gender":"M","stuID":"005716305","surveyA":null,"surveyID":"201202","surveyNum":"Resp 10","surveyQ":"Please indicate how you feel about the following statement: - The science in school is not related to my everyday life.","surveyR":"Agree"}{"gender":"F","stuID":"005716310","surveyA":null,"surveyID":"201202","surveyNum":"Resp 03","surveyQ":"Which subjects do you most like to study in school? Rank them from like it a lot to don't like it at all. - Science","surveyR":"like it a lot"}{"gender":"F","stuID":"005716310","surveyA":null,"surveyID":"201202","surveyNum":"Resp 06","surveyQ":"Please indicate how you feel about the following statement: - I would rather find out why something happens by doing an experiment than being told.","surveyR":"Agree"}
哪个没有正确的 json 语法。
我的客户端 ajax 调用如下所示:
success: function (result) {
$.parseJSON(result);
$.each(result, function (key, value) {
$('body').append(key + ': ' + value);
});
}
我不确定我是否正确调用它,但我知道 webService 不正确。