POST JSON Dictionary without Key/Value Text

后端 未结 4 457
醉梦人生
醉梦人生 2020-12-11 17:00

I have a WCF endpoint that is like such:

[OperationContract]
[WebInvoke(Method = \"POST\", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = W         


        
相关标签:
4条回答
  • 2020-12-11 17:17

    I have been searching for the same solution. I managed to get it working by using 'JavaScriptSerializer'. You have to set the function output to 'Stream' not 'String'.

    Public Function hotel_availability(ByVal data1 As Stream) As Stream Implements IMyTestAPI.hotel_availability
    ....
    Dim serializer As New JavaScriptSerializer()
    Dim serializedResult = serializer.Serialize(a_response)
    Dim json = Encoding.UTF8.GetBytes(serializedResult)
    Dim a_result as  New MemoryStream(json)
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"
    
    return a_result
    
    0 讨论(0)
  • 2020-12-11 17:20

    You basically need a SerializableDynamic Object, so that your method will look like this:

    [OperationContract]
    [WebInvoke(...)]
    Stream DoWork(SerializableDynamicObject items);
    

    You can see a good guide on how to build the SerializableDynamic Object from a Dictionary here: (see Solution section). Hope this helps...

    0 讨论(0)
  • 2020-12-11 17:23

    You may have better success using the Newtonsoft JSON serializer.

    It is available here http://www.newtonsoft.com/json for free and is also available as a NuGet package.

    I have found to be much more flexible than the stock JSON serializers.

    Also, it looks like your URITemplate is empty. I haven't used the wrapped body style, but with bare body style you need the URITemplate to be populated.

    0 讨论(0)
  • 2020-12-11 17:42

    Is it an option for you to change the DoWork parameter to a string, then use a Json deserializer in the method to convert it to the appropriate format?

    0 讨论(0)
提交回复
热议问题