【发布时间】:2011-01-17 18:28:01
【问题描述】:
在我的 JS 中,我使用 Jquery 的 $ajax 函数来调用 Web 服务并将 jsonified 数据发送给它。数据格式如下:
var countries = {
"1A": {
id: "1A",
name: "Andorra"
},
"2B": {
id: 2B
name: "Belgium"
},
..etc
};
var jsonData = JSON.stringify({ data: data });
//然后调用 $ajax 并调用 c# web 服务
在 c# 端,Web 服务需要解压这些数据,目前它以 string[][] 数据类型出现。如何将其转换为格式,以便我可以参考诸如
之类的属性.id 和 .name?假设我有一个名为 Sample 的类具有这些属性? 谢谢!
编辑:
这是我的 JS 代码:
var jsonData = JSON.stringify(countries);
$.ajax({
type: 'POST',
url: 'http://localhost/MyService.asmx/Foo',
contentType: 'application/json; charset=utf-8',
data: jsonData,
success: function (msg) {
alert(msg.d);
},
error: function (xhr, status) {
switch (status) {
case 404:
alert('File not found');
break;
case 500:
alert('Server error');
break;
case 0:
alert('Request aborted');
break;
default:
alert('Unknown error ' + status);
}
}
});
在我拥有的 c# web 服务中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Collections;
using System.IO;
using System.Web.Script.Services;
[WebMethod]
[ScriptMethod]
public string Foo(IDictionary<string, Country> countries)
{
return "success";
}
【问题讨论】:
标签: c# jquery web-services json