[英]Why does the JSON2CSharp online converter decorate each property with a [JsonProperty("...")] attribute?
I have this JSON response from an API:我有来自 API 的 JSON 响应:
{
"arguments": {
"Configuration": {
"Building_Configuration.Parameters_SP.fixtureStrategy_SP": "ETA",
"Building_Configuration.Parameters_SP.dimensionSelection_SP": "Imperial",
"Building_Configuration.Parameters_SP.controllerRobotic_SP": false,
"Building_Configuration.Parameters_SP.controllerBACNet_SP": false
}
}
}
I have this Root.cs
Model file that I used the JSON to C# Converter to make that corresponds to the JSON above in my solution in C# Visual Studio 2019:我有这个
Root.cs
模型文件,我使用JSON 到 C# 转换器制作它对应于我在 C# Visual Studio 2019 中的解决方案中的上述 JSON:
public class Root
{
public Arguments arguments { get; set; }
}
public class Arguments
{
public Configuration Configuration { get; set; }
}
public class Configuration
{
[JsonProperty("Building_Configuration.Parameters_SP.fixtureStrategy_SP")]
public string BuildingConfigurationParametersSPFixtureStrategySP { get; set; }
[JsonProperty("Building_Configuration.Parameters_SP.dimensionSelection_SP")]
public string BuildingConfigurationParametersSPDimensionSelectionSP { get; set; }
[JsonProperty("Building_Configuration.Parameters_SP.controllerRobotic_SP")]
public bool BuildingConfigurationParametersSPControllerRoboticSP { get; set; }
[JsonProperty("Building_Configuration.Parameters_SP.controllerBACNet_SP")]
public bool BuildingConfigurationParametersSPControllerBACNetSP { get; set; }
}
I'm trying to access and return the value of BuildingConfigurationParametersSPFixtureStrategySP
(the first property in the Configuration
class above) in this manner:我正在尝试以这种方式访问并返回
BuildingConfigurationParametersSPFixtureStrategySP
的值(上面的Configuration
类中的第一个属性):
public string CreateExecPost()
{
/*...everthing here works fine down through the end of this method. I can set
breakpoints and step through the following code and look at the values of all the
following variables and all is well
....*/
var payload = new StringContent(newPost, Encoding.UTF8, "application/json");
var result = client.PostAsync(endpoint, payload).Result.Content.ReadAsStringAsync().Result;
Root MyObject = JsonConvert.DeserializeObject<Root>(result);
return MyObject.arguments.configuration.BuildingConfigurationParametersSPFixtureStrategySP;
} //The correct value for BuildingConfigurationParametersSPFixtureStrategySP is returned and all is well!
So, the question is why does the converter generate an attribute like...所以,问题是为什么转换器会生成一个属性,比如......
[JsonProperty("Building_Configuration.Parameters_SP.fixtureStrategy_SP")]
...above each of the { get; set; }
...在每个
{ get; set; }
{ get; set; }
{ get; set; }
statements? { get; set; }
陈述? I've researched and read about JsonProperty
and JsonPropertyAttribute
, but I'm still not fully clear on it.我已经研究并阅读了有关
JsonProperty
和JsonPropertyAttribute
的信息,但我仍然不完全清楚。 I'm not seeing what the tool uses to signal it to generate the attribute or why it does it.我没有看到该工具使用什么信号来生成属性或它为什么这样做。
This tool generates code with the Json.net library by default, and the official documentation doesn't explain much on this class: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_JsonProperty.htm该工具默认使用Json.net库生成代码,官方文档对这个类没有过多解释: https ://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_JsonProperty.htm
There are other similar questions on how to use this, for example: What [JsonProperty] used for in c#?关于如何使用它还有其他类似的问题,例如: What [JsonProperty] used in c#?
The general usage of this class is when you want to, or need to , rename a property.此类的一般用途是当您想要或需要重命名属性时。 And the last one is relevant here.
最后一个在这里是相关的。
The json parser normally tries to match the property names 1:1 with your class properties. json 解析器通常会尝试将属性名称与您的类属性进行 1:1 匹配。 But whenever the json property name contains reserved keywords, language syntax, or otherwise illegal property names, you need to rename it.
但是每当 json 属性名称包含保留关键字、语言语法或其他非法属性名称时,您都需要对其进行重命名。
In your example the name Building_Configuration.Parameters_SP.fixtureStrategy_SP
contains periods.在您的示例中,名称
Building_Configuration.Parameters_SP.fixtureStrategy_SP
包含句点。 If you would try to name your get;set property like this, the code would not compile.如果您尝试像这样命名您的 get;set 属性,则代码将无法编译。
The site that generates the code knows this, and will add the required JsonProperty to map the full json property name to the class field that was renamed to BuildingConfigurationParametersSPFixtureStrategySP
(the illegal characters were removed)生成代码的站点知道这一点,并将添加所需的 JsonProperty 以将完整的 json 属性名称映射到已重命名为
BuildingConfigurationParametersSPFixtureStrategySP
的类字段(删除了非法字符)
See for valid property names: https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names请参阅有效的属性名称: https ://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names
And for reference: Accessing properties with a dot in their name供参考: 访问名称中带有点的属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.