我有以下从外部方收到的JSON字符串.
{
"team":[
{
"v1":"",
"attributes":{
"eighty_min_score":"",
"home_or_away":"home",
"score":"22",
"team_id":"500"
}
},
{
"v1":"",
"attributes":{
"eighty_min_score":"",
"home_or_away":"away",
"score":"30",
"team_id":"600"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的映射类:
public class Attributes
{
public string eighty_min_score { get; set; }
public string home_or_away { get; set; }
public string score { get; set; }
public string team_id { get; set; }
}
public class Team
{
public string v1 { get; set; }
public Attributes attributes { get; set; }
}
public class RootObject
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用http://json.codeplex.com/,我正在谈论基于Ruby的Rest API.问题是大多数属性都有一个ruby下划线命名约定.我想知道是否有人知道一种方式,以便我可以避免添加大量的JsonProperty.
例如,我想避免添加JsonProperty属性并在序列化程序设置中内置一个约定,以便它知道在.NET命名约定中尝试使用下划线映射属性:)
public class Member
{
[JsonProperty(PropertyName = "avatar_url")]
public string AvatarUrl { get; set; }
[JsonProperty(PropertyName = "twitter_screen_name")]
public string TwitterScreenName { get; set; }
[JsonProperty(PropertyName = "website_url")]
public string WebSiteUrl { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我正在从如下所示的Web API接收JSON数据:
[
{
"id": 1
"error_message": "An error has occurred!"
}
]
Run Code Online (Sandbox Code Playgroud)
我将此数据反序列化为以下类型的对象:
public class ErrorDetails
{
public int Id { get; set; }
[JsonProperty("error_message")]
public string ErrorMessage { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
稍后在我的应用程序中,我想再次将ErrorDetails对象序列化为JSON,但使用属性名称ErrorMessage代替error_message。因此结果将如下所示:
[
{
"Id": 1
"ErrorMessage": "An error has occurred!"
}
]
Run Code Online (Sandbox Code Playgroud)
Is there an easy way I can accomplish this with Json.Net? Perhaps using a custom resolver and some attributes like:
public class ErrorDetails
{
public int Id { get; set; …Run Code Online (Sandbox Code Playgroud) 我正在尝试从WebApi v2(完整框架,而不是点网核心)中的snake_cased JSON绑定我的PascalCased c#模型。
这是我的API:
public class MyApi : ApiController
{
[HttpPost]
public IHttpActionResult DoSomething([FromBody]InputObjectDTO inputObject)
{
database.InsertData(inputObject.FullName, inputObject.TotalPrice)
return Ok();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的输入对象:
public class InputObjectDTO
{
public string FullName { get; set; }
public int TotalPrice { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是JSON看起来像这样:
{
"full_name": "John Smith",
"total_price": "20.00"
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用JsonProperty属性:
public class InputObjectDTO
{
[JsonProperty(PropertyName = "full_name")]
public string FullName { get; set; }
[JsonProperty(PropertyName = "total_price")]
public int TotalPrice { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是我的InputObjectDTO 很大 …