ajax调用Web服务返回不正确的json

时间:2012-11-26 16:26:39

标签: vb.net json jquery

我正在使用ajax,vb和.net创建一个使用High Charts动态创建图形和图表的网页。我目前可以调用我的webservice运行并返回json但是我无法获得正确的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不正确。

1 个答案:

答案 0 :(得分:0)

这是答案的a link。我几乎一直遵循这个例子,因为缺少一个List来保存对象。