Web API控制器 - 要返回的数据的正确格式是什么

时间:2017-04-25 04:12:09

标签: json asp.net-web-api

我有Web API控制器来检索故障单信息。在开始时 - 调用API并将请求路由到适当的控制器功能。控制器将请求传递给数据库。从那里,检索到的数据通过字典块运行,其中字段名称与数据相关联。接下来,数据被序列化。然后将数据传回控制器。在这一点上,我知道Json字符串看起来很好。但是,当正确格式化的json数据传回给调用者时,会在输出中添加一些斜杠。

我的理解是Web API应该自动格式化返回数据。我怀疑在返回之前我正确地格式化了控制器的数据。

    Public Function GetTicketSearch(ByVal SourceTktNum As String) As Object

    'GET api/outage/SourceTktNum

    Dim strFullName As String = MethodBase.GetCurrentMethod().ReflectedType.FullName
    Dim strMethodName As String = MethodBase.GetCurrentMethod().Name
    Dim strClassRoutine As String = strMethodName & " / " & strFullName

    Try

        Dim objJsonRptRtn As Object = GetReportData_bllBLL.BLL__DataSet__GetReportData__GetData(strMARCLSysId, strLogonSysId, SourceTktNum)

        'AT THIS POINT I KNOW THE JSON STRING LOOKS AS IT SHOULD.

        Return objJsonRptRtn

        'AFTER THE ABOVE STATEMENT SOMETHING HAPPENS TO THE DATA / SLASHES ARE ADDED TO THE OUTPUT TO BE RETURNED BY THE API

    Catch ex As Exception

        Dim strExMessage As String = ex.Message
        Dim strStackTrace As String = ex.StackTrace

        Dim strMsg As String = strExMessage & ControlChars.CrLf & ControlChars.Lf & strStackTrace & ControlChars.CrLf & ControlChars.Lf

        MailLogEvent.BLL__Process__MailAndLogEvent__AddLogEntry(strMARCLSysId, strLogonSysId, 901020, dteTime_Start, 0, strMsg, strClassRoutine)

        Throw New HttpResponseException(HttpStatusCode.InternalServerError)

    End Try

End Function

创建要传递回控制器的JSON对象的代码......

        '--------------------------------------------------------- Create Json String

        Dim dctDataDictionary As New Dictionary(Of String, String)

        dctDataDictionary.Add("sourceTktNum", strSourceTktNumKey)
        dctDataDictionary.Add("incidentTime", strIncidentTime)
        dctDataDictionary.Add("incidentEndTime", strIncidentEndTime)
        dctDataDictionary.Add("recordTimeStamp", strRecordTimeStamp)
        dctDataDictionary.Add("outageReasonCd", strOutageReasonCd)
        dctDataDictionary.Add("numDS3", strNumDS3)
        dctDataDictionary.Add("numBlocked", strNumBlocked)
        dctDataDictionary.Add("numVOIP", strNumVOIP)
        dctDataDictionary.Add("numWireline", strNumWireline)
        dctDataDictionary.Add("numEndUserCircuits", strNumEndUserCircuits)
        dctDataDictionary.Add("stateCd", strStateCd)
        dctDataDictionary.Add("city", strCity)
        dctDataDictionary.Add("incidentDescription", strIncidentDescription)
        dctDataDictionary.Add("causeDesc", strCauseDesc)
        dctDataDictionary.Add("equipFailedDesc", strEquipFailedDesc)
        dctDataDictionary.Add("networkPartDesc", strNetworkPartDesc)
        dctDataDictionary.Add("restoreMethodDesc", strRestoreMethodDesc)

        objJsonRptRtn = New System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dctDataDictionary)

        Return objJsonRptRtn

2 个答案:

答案 0 :(得分:0)

这可能会发生,因为您再次尝试将json数据转换为json,这会产生额外的斜杠。 您可以在返回之前显示代码' objJsonRptRtn'。

答案 1 :(得分:0)

事实证明我是双序列化。我删除了序列化字典输出的语句。然后,只需将字典传回控制器,然后让控制器返回字典。一切都很好......

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate> 
<a class="fancybox" href="<%#Eval("image")%>" data-fancybox-group="gallery">
  <asp:Image ID="Image1" ImageUrl='<%#Eval("image")%>' CssClass="img-responsive" runat="server"></asp:Image></a>  
  </ItemTemplate>
 </asp:Repeater>