I’m creating a webpage to create graphs and charts on the fly with High Charts using ajax, vb and .net. I currently can call my webservice which runs and returns json however I can’t get the correct json. The function I’m using to return json can serialize it fine for one object but the way I have it setup makes more than one incorrect.
<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()
I know why it does it but I’m not sure how to fix it. I put the jsonSerializer.WriteObject(stream, prod) in the loop so it creates a json string per object. The problem is the output looks like this on the client side:
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"}
Which does not have the correct json syntax.
My client side ajax call looks like this:
success: function (result) {
$.parseJSON(result);
$.each(result, function (key, value) {
$('body').append(key + ': ' + value);
});
}
I’m not sure if I’m calling it right but I know the webService isn’t correct.
Here’s a link to the answer. I pretty much followed the example straight through it was the lack of an List to hold the object.