jquery 中json数组的操作


 

 

  Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Json对象给页面,让页面去展现。

      这一切都非常的简单,今天要学习的并非这些。我们在实际处理业务过程中,会发现往往页面要传递给webservice 的并非一个或多个字符串,有时候需要传递的是一个组合数据,如这样的一组数据:

 

  {'Employee': [{'name':'John','sex':'man','age':'25'},{'name':'Tom','sex':'man','age':'21'}]}

 

 

      客户端将这样的Json字符串作为$.ajax方法的data参数是没有问题的,然而,服务端的webservice该如何去写接收参数却成为了一个问题。在百度、谷歌了一番后,只发现提问的却没有回答的。索性还是自己去研究吧,发现其实Employee对象首先是一个数组,其次数组的每一项都是一个Dictionary<string,string>字典类型。于是我尝试在服务端使用Dictionary<string,string>[] Employee来接收客户端传递的参数,一切如我所料,成功!

客户端代码如下:

 

// JQuery 调用webService导入数据
function LoadData() {
var studentData = CollectionData();
$.ajax({
url: " ImportDataService.asmx/ImportStu " ,
type: " post " ,
contentType: " application/json;charset=utf-8 " ,
dataType: " json " ,
data: " {'students':[{'name':'KoBe ','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]} " ,
success: function(result) {
alert(result.d);
},
error: function(e) {
alert(e.responseText);
}
});
}
// JQuery 调用webService导入数据
function LoadData() {
var studentData = CollectionData();
$.ajax({
url: " ImportDataService.asmx/ImportStu " ,
type: " post " ,
contentType: " application/json;charset=utf-8 " ,
dataType: " json " ,
data: " {'students':[{'name':'KoBe ','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]} " ,
success: function(result) {
alert(result.d);
},
error: function(e) {
alert(e.responseText);
}
});
}

 

服务端代码如下:

 

/// <summary>
///
/// </summary>
/// <param name="students"></param>
/// <returns></returns>
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ImportStu(Dictionary < string , string > []students)
{
if (students.Length == 0 )
{
return " 没有任何数据! " ;
}
else
{
try
{
foreach (Dictionary < string , string > stu in students)
{
// 构造一个新的Student对象。
Student student = new Student();

// 为新构造的Student对象属性赋值。
foreach ( string key in stu.Keys)
{
switch (key)
{
case " name " :
student.Name = stu[key];
break ;
case " sex " :
student.Sex = stu[key];
break ;
case " age " :
int age;
if (Int32.TryParse(stu[key], out age))
{
student.Age = age;
}
else
{
student.Age = 0 ;
}
break ;
default :
break ;
}
}
}
return " 导入学生成功! " ;
}
catch
{
throw new Exception( " 导入学生失败! " );
}
}
}
/// <summary>
///
/// </summary>
/// <param name="students"></param>
/// <returns></returns>
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ImportStu(Dictionary < string , string > []students)
{
if (students.Length == 0 )
{
return " 没有任何数据! " ;
}
else
{
try
{
foreach (Dictionary < string , string > stu in students)
{
// 构造一个新的Student对象。
Student student = new Student();

// 为新构造的Student对象属性赋值。
foreach ( string key in stu.Keys)
{
switch (key)
{
case " name " :
student.Name = stu[key];
break ;
case " sex " :
student.Sex = stu[key];
break ;
case " age " :
int age;
if (Int32.TryParse(stu[key], out age))
{
student.Age = age;
}
else
{
student.Age = 0 ;
}
break ;
default :
break ;
}
}
}
return " 导入学生成功! " ;
}
catch
{
throw new Exception( " 导入学生失败! " );
}
}
}
posted on 2012-02-14 10:22  Jack.leung 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jack-liang/archive/2012/02/14/2350500.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值