本文转帖地址:http://qzone.qq.com/blog/25793693-1233110666
客户端访问WebService(中)
阅读(22) 评论(0) 发表时间:2009年01月28日 10:44
result.rows
}
sb.append("</table>");
$get("result").innerHTML=sb.toString();
}
function onFailed(error)
{
alert(error.get_message());
}
</script>
}
第三在Bin中引入Microsoft.Web.Preview.dll文件并在web.config中写
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview" />
<add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview" />
<add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview" />
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
演示五:自定义JavaScriptConverter
用于处理复杂类型
-处理循环引用
-简化默认的复杂序列化和反序列化行为
定义并使用一个JavaScriptConverter
-定义一个Converter继承JavaScriptConverter类
-实现SupportedTypes
-实现Serialize方法用于序列化复杂数据
-实现Deserialize方法用于反序列化复杂数据
-在web.config内注册该Converter
第一定义BoyAndGirl.cs类文件
namespace ComplexType
{
public class Boy
{
public string Name;
public Girl GirFriend;
}
public class Girl
{
public string Name;
public Boy BoyFriend;
}
}
第二定义asmx文件
[ScriptService]
public class BoyGirlService : System.Web.Services.WebService
{
[WebMethod]
public Boy GetBoyWithGirlFriend()
{
Boy boy=new Boy();
boy.Name="Terry";
Girl girl=new Girl();
girl.Name="Marry";
boy.GirlFriend=girl;
girl.BoyFriend=boy;
return boy;
}
[WebMethod]
public string SetBoyWithGirlFriend(Boy boy)
{
Debug.Assert(boy == boy.GirlFriend.BoyFriend);
return String.Format(
"It's {0}, his girlfriend is {1}",
boy.Name, boy.GirlFriend.Name);
}
主文件aspx文件的代码
function getBoy()
{
BoyGirlService.GetBoyWithGirlFriend(onGetBoySucceeded,onFailed);
}
function onGetBoySucceeded(result)
{
//result.GirlFriend.BoyFriend=result; //这就是循环引用又建立起来了
alert(String.format(
"It'a {0},his girlfriend is {1}",
result.Name,
result.GirlFriend.Name));
}
function onFailed(error)
{
alert(error.get_message());
}
出错,所以要定义一个BoyConverter.cs来协助
namespace Converter
{
public class BoyConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
Boy boy = new Boy();
boy.Name = (string)dictionary["Name"];
boy.GirlFriend = serializer.ConvertToType<Girl>(dictionary["GirlFriend"]);
boy.GirlFriend.BoyFriend = boy;
return boy;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
Boy boy = (Boy)obj;
IDictionary<string, object> result = new Dictionary<string, object>();
result["Name"] = boy.Name;
boy.GirlFriend.BoyFriend = null;
result["GirlFriend"] = boy.GirlFriend;
return result;
}
public override IEnumerable<Type> SupportedTypes
{
get
{
yield return typeof(Boy);
}
}
}
}
在web.config中定义
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name="BoyConverter" type="Converter.BoyConverter, App_Code" /> //因为cs在App.Code中
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
演示六:序列化与反序列化能力
JavaScriptConverter:自定义序列化与反序列化能力
JavaScriptTypeReSolver:建立数据类型与标识符的映射
不是真正的JSON序列化
-时间日期对象
-Sys.Serialization.JavaScriptSerializer
JavaScriptSerializer类
-RegisterConverters 注册在web.config中的Converter只能用于WebService访问中
-ConvertToType<>:
-Deserialize<>
-DeserializeObject
-Serialize
序列化反序列化不明白以后再看