【发布时间】: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