c# – 在JSON web-api上公开的对象 – 如何阻止属性名称更改大小写?

c# – 在JSON web-api上公开的对象 – 如何阻止属性名称更改大小写?,第1张

概述我有一个对象模型,看起来像: public class Product{ public string ProductCode { get; set; } public string ProductInfo { get; set; }} 我通过Dapper填充它,并将它暴露给angular.js使用者,但JSON中的属性名称如下: { "productCode": 1, 我有一个对象模型,看起来像:

public class Product{    public string ProductCode { get; set; }    public string ProductInfo { get; set; }}

我通过Dapper填充它,并将它暴露给angular.Js使用者,但JsON中的属性名称如下:

{     "productCode": 1,"productInfo": "Product number 1"}

特别注意驼峰式的情况.我希望它与原始声明的名称相匹配,即

{     "ProductCode": 1,"ProductInfo": "Product number 1"}

我怎样才能做到这一点?

解决方法 在幕后,很可能web-API使用JsON.Net作为JsON序列化引擎;这意味着您可以使用JsON.Net的属性控制输出,例如:

public class Product{    [JsonProperty("ProductCode")]    public string ProductCode { get; set; }    [JsonProperty("ProductInfo")]    public string ProductInfo { get; set; }}

没有这些,JsON.Net使用约定和配置 – 通常的JsON约定是使用camel-case,因此这是默认的.您也可以更改默认配置,但除非您了解影响的范围,否则我会建议您不要这样做.

总结

以上是内存溢出为你收集整理的c# – 在JSON web-api上公开的对象 – 如何阻止属性名称更改大小写?全部内容,希望文章能够帮你解决c# – 在JSON web-api上公开的对象 – 如何阻止属性名称更改大小写?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1215145.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存