WCF: 以Json格式返回对象
1、先建一个WCF Service
建一个ServiceContract接口 1 [ServiceContract]

public interface IJsonWCFService
{
/// <summary>
/// GetJsonResult
/// </summary>
/// <param name="name"></param>
/// <param name="address"></param>
/// <param name="phone"></param>
/// <remarks>
/// 为实现Json序列化,添加属性
/// [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
/// </remarks>
/// <returns></returns>
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
JsonResult GetJsonResult(string name, string address, string phone);
}

实现这个接口

public class JsonWCFService : IJsonWCFService
{
#region IJsonWCFService Members
/// <summary>
/// Implement the interface
/// </summary>
/// <param name="name">Name</param>
/// <param name="address">Address</param>
/// <param name="phone">PhoneNumber</param>
/// <returns>JsonResult</returns>
public JsonResult GetJsonResult(string name, string address, string phone)
{
JsonResult result = new JsonResult(name, address, phone);
return result;
}
#endregion
}

一个返回的DataContract类

[DataContract]
public class JsonResult
{
/// <summary>
/// Construct
/// </summary>
public JsonResult(string name, string address, string phone)
{
_name = string.Format("Name:{0}", name);
_address = string.Format("Address:{0}", address);
_phoneNumber = string.Format("PhoneNubmer:{0}", phone);
} private string _name;
/// <summary>
/// Name
/// </summary>
[DataMember]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _address;
/// <summary>
/// Address
/// </summary>
[DataMember]
public string Address
{
get { return _address; }
set { _address = value; }
}
private string _phoneNumber;
/// <summary>
/// PhoneNumber
/// </summary>
[DataMember]
public string PhoneNumber
{
get { return _phoneNumber; }
set { _phoneNumber = value; }
}

2、为实现Json序列化设置,我们还得添加一个WebContentTypeMapper
(此类最终会用在Service的配置文件中)

using System.ServiceModel.Channels; namespace Microsoft.Ajax.Samples
{
/// <summary>
/// JsonContentTypeMapper
/// 用在配置中<webMessageEncoding webContentTypeMapperType="Microsoft.Ajax.Samples.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
/// </summary>
public class JsonContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
if (contentType == "text/javascript")
{
return WebContentFormat.Json;
}
else
{
return WebContentFormat.Default;
}
}
}
}

3、添加svc文件,便于发布Service
svc文件其实是十分简单的一个文件,以下是SVC文件中的内容,可以将此文件添加在网站项目的根目录,也可以是一个子目录。对此没有太多的要求。
<%@ ServiceHost Language="C#" Debug="true" Service="JsonWCFService" %>
4、添加web.config文件
WCFService中相当一部分知识是关于配置的

<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web> </system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors >
<behavior name="jsonWcfBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="JsonMapper">
<!--此处配置相当重要,使用了我们编写的JsonContentTypeMapper类,约定返回值类型是Json-->
<webMessageEncoding webContentTypeMapperType="Microsoft.Ajax.Samples.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
</webMessageEncoding>
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
</bindings>
<services>
<service name="JsonWCFService" >
<!--注意此处的endpoint配置,address和contract两个属性,在客户端Js调用时会用的上-->
<endpoint address="ajaxEndpoint" behaviorConfiguration="jsonWcfBehavior"
binding="customBinding"
bindingConfiguration="JsonMapper" contract="IJsonWCFService">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
WCF: 以Json格式返回对象的更多相关文章
- JSon_零基础_005_将po(bean)对象集合List转换为JSon格式的对象字符串,返回给界面
将po(bean)对象集合List转换为JSon格式的对象字符串,返回给界面 导入jar包: 编写:po(bean)代码: package com.west.webcourse.po; /** * 第 ...
- JSon_零基础_005_将po(bean)对象转换为JSon格式的对象字符串,返回给界面
将po(bean)对象转换为JSon格式的对象字符串,返回给界面 导入jar包: 编写po(bean)类: package com.west.webcourse.po; /** * 第01步:编写be ...
- JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面
将Set集合对象转换为JSon格式的对象字符串,返回给界面 需要导入的jar包: 编写:servlet: package com.west.webcourse.servlet; import java ...
- JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面
将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...
- JSon_零基础_002_将List类型数组转换为JSon格式的对象字符串,返回给界面
将List类型数组转换为JSon格式的对象字符串,返回给界面 所需要导入的包: 编写bean: package com.west.webcourse.po; /** * 第01步:编写bean类, * ...
- 如何解决jersey框架中以json格式返回数组,当数组中元素一个时json格式不对
原文地址:http://www.cnblogs.com/swpk/p/3566536.html?utm_source=tuicool jersey 是oracle 出的一个较好的REST框架.使用此框 ...
- 总结的一些json格式和对象/String/Map/List等的互转工具类
总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...
- 把普通对象转换成json格式的对象
1.什么叫做JSON?JSON只是一种数据格式(它不是一种新的数据类型) var obj = {name: "中国", age: 5000};//->普通格式的对象 var ...
- springMVC框架下返回json格式的对象,list,map
原文地址:http://liuzidong.iteye.com/blog/1069343 注意这个例子要使用jQuery,但是jquery文件属于静态的资源文件,所以要在springMVC中设置静态资 ...
随机推荐
- hdu 5698(杨辉三角的性质+逆元)
---恢复内容开始--- 瞬间移动 Accepts: 1018 Submissions: 3620 Time Limit: 4000/2000 MS (Java/Others) Memory Limi ...
- codevs——1048 石子归并 (区间DP)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并 ...
- luogu P1579 哥德巴赫猜想(升级版)
题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...
- python的列表元组字典集合比较
定义 方法 列表 可以包含不同类型的对象,可以增减元素,可以跟其他的列表结合或者把一个列表拆分,用[]来定义的 eg:aList=[123,'abc',4.56,['inner','list'],7- ...
- linux 项目管理、服务器管理、服务器维护
代码打包:tar -zcvf ImOra.3.2.6.tgz --exclude=Public/.htaccess --exclude=Apps/Demo Apps/ Config/ Shell/ L ...
- Mac git 的使用
1. mac 安装git brew install git 2.初使化 git config --global user.name "mygit" git config --glo ...
- Difference between a Hard Link and Soft (Symbolic) Link
Within the Unix/Linux file system, linking lets you create file shortcuts to link one or more files. ...
- Android为什么方法数不能超过65535
言归正传,来聊聊为什么方法数不能超过65535?搬上Dalvik工程师在SF上的回答,因为在Dalvik指令集里,调用方法的invoke-kind指令中,method reference index只 ...
- http各类攻击及tcpcopy工具
1.专业的还得ixia.Spirent TestCenter等软硬件一体的 2.一般的使用软件的,安装在linux上使用 参考: 1.http://blog.csdn.net/wuzhimang/ar ...
- (转)python装饰器进阶一
Python装饰器进阶之一 先看例子 网上有很多装饰器的文章,上来说半天也没让人看明白装饰器到底是个什么,究竟有什么用,我们直接来看几个例子. Python递归求斐波那契数列 def fibonacc ...