wcf返回json android,WCF返回JSON的详细配置

开发环境:VS2008,c#

1.新建个WCF服务网站

文件-新建-网站-WCF服务

e6142ce650671aea75f4bcb6b0846057.png

2,运行一下,提示配置WEB.CONFIG,点击确认.

3,打开web.config增加如下节点:

endpoint 中增加 behaviorConfiguration="webBehavior"

65a929d18ba2b813d681946508edd330.png

9637ae5d9544c361c40b8a964da1d048.png

处理完以上3处之后,web.config就OK了.

4,IService.cs 增加:

[OperationContract]

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]

List Test();

Service.cs 增加:

public List Test()

{

List lst = new List();

CompositeType type = new CompositeType();

type.BoolValue = true;

type.StringValue = "22";

lst.Add(type);

CompositeType type2 = new CompositeType();

type2.BoolValue = false;

type2.StringValue = "33";

lst.Add(type2);

return lst;

}

CompositeType类:

[DataContract]

public class CompositeType

{     bool boolValue = true;     string stringValue = "Hello ";

[DataMember]

public bool BoolValue     {         get { return boolValue; }         set { boolValue = value; }     }

[DataMember]

public string StringValue     {         get { return stringValue; }         set { stringValue = value; }     }

}

5,Service.cs 增加

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

46acffa34c35a057de9eb099c72bc7d8.png

6,运行测试,如:

http://localhost:1177/WCFData/Service.svc

手动输入:http://localhost:1177/WCFData/Service.svc/Test

返回:[{"BoolValue":true,"StringValue":"22"},{"BoolValue":false,"StringValue":"33"}]

测试成功!

原文:http://www.cnblogs.com/gaibangdaxia/p/5730382.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值