ajax、vb、および.netを使用して、ハイチャートでオンザフライでグラフとチャートを作成するためのWebページを作成しています。現在、json を実行して返す Web サービスを呼び出すことができますが、正しい json を取得できません。jsonを返すために使用している関数は、1つのオブジェクトに対して正常にシリアル化できますが、セットアップ方法によって複数のオブジェクトが正しくなくなります。
<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 が正しくないことはわかっています。