我正在尝试反序列化以下json
{
"category": "mycategory",
"updated_date": "2011-11-10T03:07:43",
"description":"description",
"image": "someImageUrl",
"longitude": 100,
"latitude": 100,
"state": 1,
"user": "Mike",
"created_date": "2011-11-10T03:07:43",
"active": true,
"id": 1,
"resource_uri": "something"
}
进入对象
public class Report
{
[JsonProperty(PropertyName = "category")]
public object Category { get; set; }
[JsonProperty(PropertyName = "updated_date")]
public string UpdatedDate { get; set; }
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
[JsonProperty(PropertyName = "longitude")]
public double Longitude { get; set; }
[JsonProperty(PropertyName = "latitude")]
public double Latitude { get; set; }
[JsonProperty(PropertyName = "state")]
public int State { get; set; }
[JsonProperty(PropertyName = "user")]
public object User { get; set; }
[JsonProperty(PropertyName = "created_date")]
public string CreatedDate { get; set; }
[JsonProperty(PropertyName = "active")]
public bool Active { get; set; }
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "resource_uri")]
public string ResourceURI { get; set; }
[JsonProperty(PropertyName = "image")]
public string Image { get; set; }
}
如果我使用像
这样的东西Report result = JsonConvert.DeserializeObject<Report>(data);
它工作正常,所有属性都应该反序列化。
但是如果我尝试在REST WEB API中使用这个类,那么它的启动行为很奇怪。序列化工作得很好?但反序列化失败:其PropertyName中所有带有“_”的三个属性都不反序列化。它们总是空的。 据我所知,它是设置和配置中的东西,但我无法弄清楚到底是什么。
修改 JS代码
$.ajax(uri,
type:'PUT',
data: {
category: "someCategory",
updated_date: "2012-11-10T03:07:43",
description: "desc3",
image: "someImgaeUrl",
longitude: 10,
latitude: 20,
state: 1,
user: "Mike",
created_date: "2010-11-10T03:07:43",
active: false,
id: 0,
resource_uri: "somethring"
})
C#代码:
[Route("{id}")]
[HttpPut]
public HttpResponseMessage Report(int id, [FromBody]ReportModel report)
{ //Do Something}