.net WFC json响应返回非json格式的数据

时间:2016-06-30 00:31:37

标签: .net sql-server json vb.net wcf

我有一个当前正在返回json数据的系统,我有一个请求返回非格式化的json数据。

目前的回应是

  

{“GetBlendResult”:[{“age”:“0”,“dateofsomething”:“23/09/1951 12:00:00   AM“,”firstname“:”Henry James“,”otherdatedate“:”24/09/1951 12:00:00   AM“,”位置“:”在和   围绕 “ ”姓“: ”布朗“, ”类型“: ”蓝“},{ ”时代“: ”64“, ”dateofsomething“:” 18/05/1950   12:00:00 AM“,”firstname“:”Herbert James“,”otherdatedate“:”20/05/1950   12:00:00 AM“,”“location”:“建筑物   J”, “姓”: “布朗”, “类型”: “绿色”}]}

然而,客户想要如上所述,但包含在某些文本中

  

jsoncallback({ “GetBlendResult”:[{ “年龄”: “0”, “dateofsomething”:“23/09/1951   12:00:00 AM“,”firstname“:”Henry James“,”otherdatedate“:”24/09/1951   12:00:00 AM“,”location“:”在和   围绕 “ ”姓“: ”布朗“, ”类型“: ”蓝“},{ ”时代“: ”64“, ”dateofsomething“:” 18/05/1950   12:00:00 AM“,”firstname“:”Herbert James“,”otherdatedate“:”20/05/1950   12:00:00 AM“,”“location”:“建筑物   J”, “姓”: “布朗”, “类型”: “绿色”}]})

代码的显着位于下方。

<OperationContract> _
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json,BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="getBlend?surname={surname}&firstname={firstname}&othernames={othername}&yearfrom={yearfrom}&yearto={yearto}")>

Function GetBlend(ByVal surname As String, ByVal firstname As String, ByVal othername As String, ByVal yearfrom As String, ByVal yearto As String) As List(Of RestBlend)


Public Function GetBlend(ByVal surname As String, ByVal firstname As String,     ByVal othername As String, ByVal yearfrom As String, ByVal yearto As String) As    List(Of RestBlendDeceased) Implements IService.GetBlendDeceased
    Dim myConn As SqlConnection
    Dim myCmd As SqlCommand
    Dim myReader As SqlDataReader
    Dim results As String = String.Empty
    Dim qrystr1 As String = String.Empty
    Dim qrystr2 As String = String.Empty

    Dim returndata As New List(Of RestBlendDeceased)

    myConn = New SqlConnection(DatabaseHelpers.conn)

'query to retrieve data 

        Do While myReader.Read()
            Dim aRestBlend As New RestBlend

            aRestBlendDeceased.age = myReader("age").ToString
            aRestBlendDeceased.firstname = myReader("field1").ToString
            aRestBlendDeceased.surname = myReader("field2").ToString
            aRestBlendDeceased.dateofsomething = myReader("field3").ToString
            aRestBlendDeceased.otherdate = myReader("field4").ToString
            aRestBlendDeceased.location = myReader("location").ToString
            aRestBlendDeceased.type = myReader("type").ToString
            returndata.Add(aRestBlend)
        Loop

        myReader.Close()
        myConn.Close()


    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        If myConn.State <> ConnectionState.Closed Then myConn.Close()
    End Try

    Return returndata

End Function

我还有许多其他json合同正在处理这项服务,但这个合同需要有所不同。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

正如Tim评论的那样,获取非json格式并不常见,在客户端上获取json和格式会非常容易,但是......

无论如何,你可以获得序列化的json并输入一个格式化的字符串,如下所示:

Dim jsonStr As String = JsonConvert.SerializeObject(returndata)
Return String.Contact("jsoncallback(", jsonStr , ")")

希望有所帮助