简体   繁体   中英

Why does the JSON2CSharp online converter decorate each property with a [JsonProperty("...")] attribute?

I have this JSON response from an API:

{
    "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:

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:

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; } statements? I've researched and read about JsonProperty and JsonPropertyAttribute , but I'm still not fully clear on it. 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

There are other similar questions on how to use this, for example: What [JsonProperty] used for 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. But whenever the json property name contains reserved keywords, language syntax, or otherwise illegal property names, you need to rename it.

In your example the name Building_Configuration.Parameters_SP.fixtureStrategy_SP contains periods. If you would try to name your get;set property like this, the code would not compile.

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)

See for valid property 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM