我想从我的客户端POST更新对象到REST服务器API。我正在使用RestSharp,并将我的对象的JSON表示添加到我的请求的正文中。但是,我的序列化对象的字符串表示格式错误。服务器拒绝它。JSON序列化输出有C#转义格式
我的要求看起来是这样的(我用的提琴手得到它)
PUT https://myapp.net/api/PriceListItems/151276 HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp/104.4.0.0
Content-Type: application/json
Host: myapp.net
Content-Length: 75
Accept-Encoding: gzip, deflate
"{\"Id\":151276,\"AgendaId\":0,\"CurrencyId\":1,\"Code\":\"\",\"Price\":7.0}"
我试图序列化我的对象与Json.NET,RestSharp的内部JSON序列,并与JavaScriptSerializer从System.Web.Script .Serialization。所有返回这种格式的字符串。我知道这样的格式化的原因是因为C#转义双引号,所以它可以正确显示它里面,但我不明白我应该如何将它传递给我的请求没有这种转义格式。我知道服务器接受正确形成的JSON的事实。
,我试图序列看起来像这样
public class PriceListItem
{
public static PriceListItem CreatePriceListItem(int id, int agendaId, int currencyId, string code, string unit, decimal price)
{
var priceListItem = new PriceListItem
{
Id = id,
AgendaId = agendaId,
CurrencyId = currencyId,
Code = code,
Price = price
};
return priceListItem;
}
public int Id { get; set; }
public int AgendaId { get; set; }
public int CurrencyId { get; set; }
public string Code { get; set; }
public decimal Price { get; set; }
编辑对象: 动了我的解决方案,从这里到答案。
好像你有双重序列化。你不能显示你的控制器的PUT动作吗? – Ghukas
是的,双重序列化正是我的问题。我只是将信息添加到我的原始帖子中以包含该信息。谢谢。 – filipv
@filipv你可能想将你的解决方案(它被双序列化)转化为答案并接受它。那么在将来有这个问题的人会更好地找到你的解决方案。 –