c# - WCF dynamic json to Dictionary -


I am using ASPX 4.5.
The client sends a JSON object with a dynamic field (can be different each time)

  Function Collection DataAnSession (formData) {var data = {}; Data ["formData"] = formdata; $ .ajax ({url: "MY_URL / StoreFormData", type: "post", data: JSON.stringify (data), contentType: 'application / json', datatype: 'json', success: function (data, textStatus , Xhr) {console.log (data); console.log ("success");}, error: work (xhr, textStatus, errorThrown) {console.log ("failure");}}); }   

On the server side, I'm trying to convert that Jason to a dictionary, but I'm getting error 500. Public String StoreFormData (Dynamic FormData) {JavaScriptSerializer} [WebSite] Jss = new JavaScriptSerializer (); Dictionary & lt; String, string & gt; FormValues ​​= jss.Deserialize & lt; Dictionary & lt; String, string & gt; & Gt; (Formadata); Return "Aaaa"; }

What am I doing?

As you want your method to get raw data in the ultimate, To apply:

  [OperationContract] [WebInvoke (method = "post", UriTemplate = "form / data", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Bodystyle = WebMessageBodyStyle.WrappedRequest]] Public String StoreFormData (Stream FileContents) {Using (StreamReader Reader = New StreamReader (fileContents)) {JavaScriptSerializer jsSerializer = New JavaScriptSerializer (); // Here is your JSON, jsonData = reader.ReadToEnd () received from client string; // I think it is better for you to use Newtonsoft. Jason Library This will allow you to parse more complex data / Jobbject data = Jobbject.Parse (jsonData); // Dictionary & lt; String, string & gt; FormValues ​​= jss.Deserialize & lt; Dictionary & lt; String, string & gt; & Gt; (Formadata); } Return "Aha" "}}   

In this way you will be able to get a side Jason data the way the client was created on the side, then you need them the way you want them.

1 Edit:

You need to change the URL, URITemplate = "form / data" Do not forget to.

Edit 2:

I think it's better in your case to use the Newtonsoft.Json library to make it Over Will allow you to parse complex data:

  Jobka data = jobs. Purse (jsonData);    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -