首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回不带转义字符的原始JSON字符串

返回不带转义字符的原始JSON字符串
EN

Stack Overflow用户
提问于 2011-11-19 00:43:27
回答 1查看 1.4K关注 0票数 0

我正在查看这篇文章:Returning raw json (string) in wcf。我想我遇到了一些相同的问题我有一个rest服务,returnS JSON,请看下面的代码:

IRestServiceImpl.vb

代码语言:javascript
复制
Imports System.ServiceModel
Imports System.ServiceModel.Web

Namespace RestService
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IRestServiceImpl" in both code and config file together.
<ServiceContract()> _
Public Interface IRestServiceImpl
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Xml, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="xml/{id}")> _
    Function XMLData(ByVal id As String) As String


    'WebMessageBodyStyle.Bare WAS WebMessageBodyStyle.wrapped
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="api/objects/json/?lat={lat}&lon={lon}&radius={radius}&cat={cat}")> _
    Function JSONData(ByVal lat As String, ByVal lon As String, ByVal radius As String, ByVal cat As String) As String
    'radius in meters


End Interface
End Namespace

RestServiceImpl.vb

代码语言:javascript
复制
Namespace RestService
Public Class RestServiceImpl
    Implements IRestServiceImpl


    Public Function XMLData(ByVal id As String) As String _
        Implements IRestServiceImpl.XMLData

        Return "XML You requested product " & id

    End Function

    Public Function JSONData(ByVal lat As String, ByVal lng As String, ByVal d As String, ByVal cat As String) As String _
        Implements IRestServiceImpl.JSONData

        'returns the results JSON in format

        'Return "JSON lat=" + lat + " lng=" + lng + " d=" + d + " cat=" + cat
        Dim sBuilder As New StringBuilder

        sBuilder.Append("{""hotspots"": [")
        sBuilder.Append("{""id"": ""test_1"",")
        sBuilder.Append("""anchor"": { ""geolocation"": { ""lat"": 52.3729, ""lon"": 4.93 } },  ")
        sBuilder.Append("""text"": {")
        sBuilder.Append("""title"": ""The Layar Office"", ")
        sBuilder.Append("""description"": ""The Location of the Layar Office"", ")
        sBuilder.Append("""footnote"": ""Powered by Layar"" },")
        sBuilder.Append("""imageURL"": ""http:\/\/custom.layar.nl\/layarimage.jpeg"",")
        sBuilder.Append("}")
        sBuilder.Append("],")
        sBuilder.Append("""layer"": ""mytest"",")
        sBuilder.Append("""errorString"": ""ok"", ")
        sBuilder.Append("""errorCode"": 0")
        sBuilder.Append("} ")

        Return sBuilder.ToString

    End Function

End Class
End Namespace

基于上面的代码,我得到了以下响应:

在Chrome浏览器中会得到这样的响应:{"JSONDataResult":"{\"hotspots\":{\"id\":\"test_1\",\"anchor\":{ \"geolocation\":{ \"lat\":52.3729,\"lon\":4.93 } },\"text\":{\"title\":\“Layar Office\",\"description\":\”Layar Office位置“,\"footnote\":\"Powered by Layar\“},\"imageURL\":\"http:\/\/custom.layar.nl\/layarimage.jpeg\",},\"layer\":\"mytest\",\"errorString\":\"ok\",\"errorCode\":0}”}

我认为反斜杠在我的响应中是因为另一个线程中描述的问题(因为我使用的是WebMessageFormat.Json)。

但是我不确定如何实现http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspxhttp://msdn.microsoft.com/en-us/library/ms789010.aspxhttp://msdn.microsoft.com/en-us/library/cc681221(VS.90).aspx上提供的代码示例

我现在将我的Irestserviceimpl.vb更改为:

代码语言:javascript
复制
Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO

Namespace RestService
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IRestServiceImpl" in both code and config file together.
<ServiceContract()> _
Public Interface IRestServiceImpl
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Xml, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="xml/{id}")> _
    Function XMLData(ByVal id As String) As String


    'WebMessageBodyStyle.Bare WAS WebMessageBodyStyle.wrapped
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare, UriTemplate:="api/objects/json/?lat={lat}&lon={lon}&radius={radius}&cat={cat}")> _
    Function JSONData(ByVal lat As String, ByVal lon As String, ByVal radius As String, ByVal cat As String) As String
    'radius in meters
End Interface


Public Class RawService
    <OperationContract(), WebGet()> _
    Public Function GetValue() As System.IO.Stream
        Dim result As String = "Hello world"
        Dim resultBytes As Byte() = Encoding.UTF8.GetBytes(result)
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"
        Return New MemoryStream(resultBytes)
    End Function
End Class

End Namespace

但我仍然不确定如何调用url或在哪里放置代码exactly...if,任何人都可以帮助我在这里开始?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-19 03:43:45

您的功能是创建一个庞大的字符串,当它被转换为Json时,它将创建一个JSON元素(这是正确的说法吗?)只有一个属性"JSONDataResult“,该属性只有一个值=您创建的字符串(所有这些引号现在都已转义)。

你做这件事的方式看起来很难!您尝试过使用WCFWebApi吗?它很容易实现,并且非常容易返回JSON或XML (并为您处理反序列化)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8185803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档