使用C#.NET JSON web服务的制作 [英] Making of JSON Webservice using C# .NET

查看:173
本文介绍了使用C#.NET JSON web服务的制作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使JSON web服务在C#.NET。 JSON字符串是由Web方法返回,但它包含了像XML结构:

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.

所以,我的问题是,是不是在纯粹的形式返回JSON字符串。我不希望出现这种XML版本和xmlns字符串。我打算稍后消耗在Android上的Web服务。

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.

谁能帮助我?

感谢

推荐答案

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

的问题是,然而,WCF的先天DataContractJsonSerializer并非总是返回正确JSON。它的词典序列化是有问题的,在最好的,因为它是序列化的键/值对的列表。为了解决这个问题一个人用手从服务方法返回并做序列化(使用Json.NET或ServiceStack进行序列化)。在这种情况下,它可能是建议使用的WebAPI,但对于一些情况下定期的WCF可以使用上述的装饰品使用。

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.

这篇关于使用C#.NET JSON web服务的制作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆