简体   繁体   English

为什么 JSON2CSharp 在线转换器用 [JsonProperty("...")] 属性装饰每个属性?

[英]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.我已经研究并阅读了有关JsonPropertyJsonPropertyAttribute的信息,但我仍然不完全清楚。 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.

相关问题 json2csharp 生成的类导致 Web 服务调用失败 - json2csharp generated class causing webservice call to fail 忽略自定义 JSON 转换器中具有属性的属性 - Ignore property with attribute in custom JSON converter JSON to Model属性绑定使用JsonProperty - JSON to Model property binding using JsonProperty 按属性名称和 JsonProperty 将 JSON 反序列化为对象 - Deserialize JSON into a Objects by property name aswell as JsonProperty CSharp-&gt;属性获取器表达式不适用于Condition - CSharp -> Property getter expression does not work with Condition 属性 [JsonProperty] 用于重命名 C# 中 DTO class 的属性 - Attribute [JsonProperty] for rename property of DTO class in C# 如何在派生类中设置Json.Net JsonProperty属性的一部分,而不替换基础中的JsonProperty? - How to set part of Json.Net JsonProperty attribute in a derived class without replacing the JsonProperty in the base? 为什么Newtonsoft.JSON在每个元素的每个属性中都加上一个&#39;$&#39;符号? - Why does Newtonsoft.JSON put a '$' sign in each property of each element? JSON.NET忽略了没有JsonProperty属性的所有属性 - JSON.NET ignores all my properties without JsonProperty attribute 使用 JsonProperty 属性中的指定路径将 C# 类序列化为嵌套的 JSON - Serialize C# class into nested JSON with specified paths in JsonProperty attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM