[英]Getting the data inside the C# web service from Jsonified string
In my JS I use Jquery's $ajax functions to call the web service and send the jsonified data to it. 在我的JS中,我使用Jquery的$ ajax函数调用Web服务并将JSON化的数据发送给它。 The data is in the following format: 数据格式如下:
var countries = {
"1A": {
id: "1A",
name: "Andorra"
},
"2B": {
id: 2B
name: "Belgium"
},
..etc
};
var jsonData = JSON.stringify({ data: data });
//then $ajax is called and it makes the call to the c# web service //然后调用$ ajax并调用c#Web服务
On the c# side the web service needs to unpack this data, currently it comes in as string[][] data type. 在C#端,Web服务需要解压缩此数据,当前它以string [] []数据类型进来。 How do I convert it to the format so I can refer to the properties such as 如何将其转换为格式,以便可以引用诸如
.id and .name? .id和.name? Assuming I have a class called Sample with these properties? 假设我有一个名为Sample的具有这些属性的类? Thanks! 谢谢!
EDIT: 编辑:
Here is my JS code: 这是我的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);
}
}
});
inside c# web service I have: 在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";
}
I don't know what string[][]
you are talking about and where does it come from but it is better to work with strong types. 我不知道您在说什么string[][]
以及它是从哪里来的,但是最好使用强类型。 So define a custom type that will model your data: 因此,定义一个将对数据建模的自定义类型:
public class Country
{
public string Id { get; set; }
public string Name { get; set; }
}
and then have the web method take a dictionary: 然后让web方法使用字典:
[WebMethod]
[ScriptMethod]
public string Foo(IDictionary<string, Country> countries)
{
// Here you will have countries["1A"].Name = "Andorra" and
// countries["2B"].Name = Belgi
...
return "success";
}
and then simply call this method (see UPDATE
for proper data): 然后只需调用此方法(有关正确数据,请参见UPDATE
):
var data = { "1A": { id: "1A", name: "Andorra" }, "2B": { id: 2B name: "Belgi" } };
$.ajax({
type: 'POST',
url: '/myservice.asmx/Foo',
contentType: 'application/json; charset=utf-8',
data: { JSON.stringify(data) },
success: function(msg) {
alert(msg.d);
}
});
UPDATE: 更新:
Actually the request should be formatted like this: 实际上,请求的格式应为:
var data = {
countries: {
"1A": {
id: "1A",
name: "Andorra"
},
"2B": {
id: "2B",
name: "Belgium"
}
}
};
Have you tried the JavaScriptSerializer ? 您是否尝试过JavaScriptSerializer ? As you said, assuming you have a class with these properties, it should/might be able to deserialize the JSON string to that type. 如您所说,假设您有一个具有这些属性的类,则它应该/可能能够将JSON字符串反序列化为该类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.