更新2010年10月19日 我知道我刚才问过这个问题,但这些答案中显示的解决方法难以令人满意,对许多人来说,这仍然是一个常见的问题.WCF只是不灵活.我创建了自己的开源C#库,用于在没有WCF的情况下创建REST服务.检查restcake.net或rest.codeplex.com以获取有关所述库的信息. 结束更新
更新8/2/2012 ASP.NET Web API(以前是WCF Web API,REST WCF的替代品)默认情况下 使用Json.NET END UPDATE
在DataContractJsonSerializer无法处理许多情况下是Json.Net在正确配置(具体周期)处理就好了.
一个服务方法可以返回一个特定的对象类型(在这种情况下是一个DTO),在这种情况下DataContractJsonSerializer将使用它,或者我可以让该方法返回一个字符串,并自己使用Json.Net进行序列化.问题是当我返回一个json字符串而不是一个对象时,发送到客户端的json用引号括起来.
使用DataContractJsonSerializer,返回特定的对象类型,响应是:
{"Message":"Hello World"}
使用Json.Net返回json字符串,响应是:
"{\"Message\":\"Hello World\"}"
我不想在客户端上使用eval()或JSON.parse()结果,如果json以字符串形式返回,用引号括起来就是我必须要做的.我意识到这种行为是正确的; 这不是我想要/需要的.我需要原始的json; 当服务方法的返回类型是对象而不是字符串时的行为.
那么,我如何让我的方法返回一个对象类型,但不能使用DataContractJsonSerializer?我怎么能告诉它使用Json.Net序列化器呢?
或者,有没有直接写入响应流?所以我可以自己退回原始的json?没有包装报价?
这是我的人为例子,供参考:
[DataContract]
public class SimpleMessage
{
[DataMember]
public string Message { get; set; }
}
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class PersonService
{
// uses DataContractJsonSerializer
// returns {"Message":"Hello World"}
[WebGet(UriTemplate = "helloObject")] …Run Code Online (Sandbox Code Playgroud) 我正在使用.Net framework 2.0/jQuery来对2.0 Web服务进行Ajax调用.无论我在ajax调用中将contentType设置为什么,服务始终返回XML.我想让它回归Json!
这是电话:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "DonationsService.asmx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Hide the fake progress indicator graphic.
$('#RSSContent').removeClass('loading');
// Insert the returned HTML into the <div>.
$('#RSSContent').html(msg.d);
}
});
});
Run Code Online (Sandbox Code Playgroud)
以下是Fiddler中请求标头的样子:
POST /DonationsService.asmx/GetDate HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: en-us
Referer: http://localhost:1238/text.htm
Accept: application/json, text/javascript, */*
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; eMusic DLM/4; .NET CLR 2.0.50727)
Host: …Run Code Online (Sandbox Code Playgroud) 我试图让ajax得到一个代码背后的webmethod.问题是我一直从jQuery onfail方法得到错误"parserror" .
如果我将GET更改为POST,一切正常.请参阅下面的代码.
Ajax Call
<script type="text/javascript">
var id = "li1234";
function AjaxGet() {
$.ajax({
type: "GET",
url: "webmethods.aspx/AjaxGet",
data: "{ 'id' : '" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
alert("success");
},
error: function(msg, text) {
alert(text);
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
代码背后
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true,
ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public static string AjaxGet(string id)
{
return id;
}
Run Code Online (Sandbox Code Playgroud)
Web.config文件
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
Run Code Online (Sandbox Code Playgroud)
正在使用的URL
......../webmethods.aspx/AjaxGet {%20%27id%27%20:%20%27li1234%27}?
作为响应的一部分,它返回页面webmethods的html.
任何帮助将不胜感激.
我意识到这里已经有很多类似的问题,但我无法想出这个问题.
我有一个Web服务(C#,.net 3.5).您需要了解的基本代码如下:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WSMember : System.Web.Services.WebService {
public WSMember () {
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetMember(string IdMember)
{
//Ignore the parameter for now... I will be looking up a database with it...
//For now just return a minimal object:
Member m = new Member();
m.Surname = "Smith";
m.FirstName = "John";
return new JavaScriptSerializer().Serialize(m);
}
Run Code Online (Sandbox Code Playgroud)
另外,在web.config中,我做了以下添加(我刚刚在其他帖子中看到过......这是正常/安全吗?)
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices> …Run Code Online (Sandbox Code Playgroud) 如何将json格式的jqGrid数据发送到服务器?我是否必须使用任何外部库或脚本来实现这一目标?
谢谢!
update1:extra licensePlateNumber不应该在那里
[
{
"licensePlateNumber": ""
},
{
"licensePlateNumber": "0000000000000029000721804",
"sku": "795127",
"description": "",
"caseQuantity": "24",
"isHeld": "false",
"expirationDate": "Jul 22, 2010 12:00:00 AM"
},
{
"licensePlateNumber": "0000000000000029000722323",
"sku": "795127",
"description": "",
"caseQuantity": "24",
"isHeld": "false",
"expirationDate": "Jul 22, 2010 12:00:00 AM"
},
{
"licensePlateNumber": "0000000000000029000722669",
"sku": "795127",
"description": "",
"caseQuantity": "24",
"isHeld": "false",
"expirationDate": "Jul 22, 2010 12:00:00 AM"
}
]
Run Code Online (Sandbox Code Playgroud) 我使用多个转换库将 xml 转换为 JSON,得到以下结果。如您所见,属性名称属性丢失了,项目名称属性也丢失了。为什么?
有没有人建议我如何更改我的 XML 以使其更易于转换?
<Asset name="xyz">
<Property name="p1">Value 1</Property>
<Property name="p2">Value 2</Property>
<TimeSeries name="TimeSeries Name 1">
<Item name="30 Apr 2009">97.47219</Item>
<Item name="01 May 2009">97.16496</Item>
<Item name="05 May 2009">97.34606</Item>
</TimeSeries>
</Asset>
Run Code Online (Sandbox Code Playgroud)
返回:
{
"Asset": {
"@attributes": {
"name": "xyz"
},
"Property": ["Value 1", "Value 2"],
"TimeSeries": {
"@attributes": {
"name": "TimeSeries Name 1"
},
"Item": ["97.47219", "97.16496", "97.34606"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法,但 XML 和 JSON 都更加冗长:
<Asset name="xyz">
<Property><name>p1</name><value>Value 1</value></Property>
<Property><name>p2</name><value>Value 2</value></Property>
<TimeSeries name="TimeSeries Name 1">
<Item><date>30 Apr …Run Code Online (Sandbox Code Playgroud) 我使用asmx从DB检索一些数据,
public class TestPage1
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
[WebMethod]
public EntityLayer.TestPage1 GetData(int id)
{
TestPage1 test = TestPage1.GetData(id).SingleOrDefault();
return test;
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebService.asmx/GetData",
data: "{id}",
dataType: "json"
});
Run Code Online (Sandbox Code Playgroud)
如何在javascript中对测试对象进行去序列化?还有更好的方法吗?谢谢