遵循 Ruby 属性命名约定的 C# JSON.NET 约定?

编程入门 行业动态 更新时间:2024-10-19 20:21:56
本文介绍了遵循 Ruby 属性命名约定的 C# JSON.NET 约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 http://json.codeplex/ 并且我正在与基于 Ruby 的休息API.问题是大多数属性都有一个 ruby​​ 下划线命名约定.我想知道是否有人知道一种方法,这样我就可以避免添加大量 JsonProperty.

I am using http://json.codeplex/ and I am talking to a Ruby based Rest API. Problem is that most of the properties have a ruby underscore naming convention. I am wondering if anyone knows of a way so that I can avoid having to Add lots of JsonProperty.

例如,我想避免添加 JsonProperty 属性并在序列化程序设置中内置了一个约定,以便它知道尝试将带有下划线的属性映射到 .NET 命名约定:)

For example I want to avoid adding the JsonProperty attribute and have a convention built into the serializer settings so that it knows to try and map properties with an underscore in the to the .NET naming convention :)

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; }
}

推荐答案

更新 - 2016 年 9 月:

Json.NET 9.0.1 具有 SnakeCaseNamingStrategy.您可以使用它来自动拥有 twitter_screen_name 样式属性.

Json.NET 9.0.1 has SnakeCaseNamingStrategy. You can use that to have twitter_screen_name style properties automatically.

DefaultContractResolver 继承并覆盖 ResolvePropertyName 以根据需要格式化属性名称.

Inherit from DefaultContractResolver and override ResolvePropertyName to format property names as you'd like.

CamelCasePropertyNamesContractResolver 对属性名称进行了类似的全局更改.

CamelCasePropertyNamesContractResolver does a similar global change to property names.

这篇关于遵循 Ruby 属性命名约定的 C# JSON.NET 约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 17:05:13,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   Ruby   NET   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!