【问题标题】:.net WFC json response to return non json formatted data.net WFC json 响应返回非 json 格式的数据
【发布时间】:2016-06-30 00:31:36
【问题描述】:

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

目前的反应是

{"GetBlendResult":[{"age":"0","dateofsomething":"23/09/1951 12:00:00 AM","firstname":"Henry James","otherdatedate":"24/09/1951 12:00:00 AM","位置":"在和 around","surname":"Brown","type":"Blue"},{"age":"64","dateofsomething":"18/05/1950 12:00:00 AM","firstname":"Herbert James","otherdatedate":"20/05/1950 12:00:00 AM","位置":"大楼 J","surname":"棕色","type":"绿色"}]}

但客户希望如上但包含在一些文本中

jsoncallback({"GetBlendResult":[{"age":"0","dateofsomething":"23/09/1951 12:00:00 AM","名字":"Henry James","otherdatedate":"24/09/1951 12:00:00 AM","位置":"在和 around","surname":"Brown","type":"Blue"},{"age":"64","dateofsomething":"18/05/1950 12:00:00 AM","firstname":"Herbert James","otherdatedate":"20/05/1950 12:00:00 AM","位置":"大楼 J","surname":"Brown","type":"Green"}]})

代码的主要部分如下。

<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 合同在这项服务上工作,但这个需要不同。任何帮助将不胜感激。

【问题讨论】:

  • 客户是否有充分的理由希望它采用非 JSON 格式?你总是可以把它作为一个简单的字符串返回。

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


【解决方案1】:

正如 Tim 所说,获得非 json 格式并不常见,在客户端获得 json 和格式非常容易,但是......

无论如何,您可以获取序列化的 json 并放入格式化字符串,如下所示:

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 2013-03-11
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2019-02-16
    • 2011-06-11
    相关资源
    最近更新 更多