Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making of JSON Webservice using C# .NET

I am trying to make JSON webservice in C# .NET. A json string is returning by web method but it contains xml structure like:

  <string xmlns="http://tempuri.org/">
  {"checkrecord":[{"rollno":"abc2","percentage":40,"attended":12,"missed":34}],"Table1":[]}
  </string> 

I saw this article before it wasn't much helpful for me.

So my problem is, that json string is not returned in its pure format. I do not want that xml version and xmlns string. I plan to consume the web service on Android later.

Can anyone help me?

Thanks

like image 320
Parth Doshi Avatar asked Feb 02 '26 05:02

Parth Doshi


1 Answers

If you decorate your interface with attributes for request and response format you can get standard WCF to return and interpret proper json.

    [WebGet(UriTemplate = "user/{userid}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

The problem is, however, that WCF's innate DataContractJsonSerializer does not always return proper json. Its serialization of dictionaries is problematic at best, since it is serialized as a list of key/value-pairs. To remedy this one has to return Stream from the service methods and do the serialization by hand (using Json.NET or ServiceStack to perform the serialization). In such cases it is probably advisable to use WebAPI, but for some cases regular WCF can be used using the mentioned decorations.

like image 180
faester Avatar answered Feb 03 '26 18:02

faester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!