问题描述
我有一个看起来像这样的对象模型:
I have an object model that looks like:
public class Product
{
public string ProductCode { get; set; }
public string ProductInfo { get; set; }
}
我正在通过Dapper填充它,并将其暴露给angular.js使用者,但是JSON中的属性名称显示为:
I'm populating this via Dapper, and exposing it to an angular.js consumer, but the property names in the JSON are coming out as:
{
"productCode": 1,
"productInfo": "Product number 1"
}
请特别注意骆驼的情况.我希望它与原始声明的名称匹配,即
Note in particular the camel-case. I would like it to match the original declared names, i.e.
{
"ProductCode": 1,
"ProductInfo": "Product number 1"
}
我该怎么做?
推荐答案
在幕后,最有可能Web-API使用JSON.Net作为JSON序列化引擎.这意味着您可以使用JSON.Net的属性控制输出,例如:
Under the hood, it is most likely that the web-API is using JSON.Net as the JSON serialization engine; this means that you can control the output using JSON.Net's attributes, for example:
public class Product
{
[JsonProperty("ProductCode")]
public string ProductCode { get; set; }
[JsonProperty("ProductInfo")]
public string ProductInfo { get; set; }
}
如果没有这些,JSON.Net将使用约定和配置-常规的JSON约定是来使用驼峰式大小写,因此这是默认设置.您还可以 更改默认配置,但是我建议您不要这样做,除非您了解影响的范围.
Without these, JSON.Net uses conventions and configuration - and the usual JSON convention is to use camel-case, hence that is the default. You can also probably change the default configuration, but I would advise against that unless you understand the scope of the impact.
这篇关于在JSON Web-API上公开的对象-如何停止属性名称的大小写更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!