簡體   English   中英

為什么 JSON2CSharp 在線轉換器用 [JsonProperty("...")] 屬性裝飾每個屬性?

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

我有來自 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
        }
    }
}

我有這個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; }
}

我正在嘗試以這種方式訪問​​並返回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!

所以,問題是為什么轉換器會生成一個屬性,比如......

[JsonProperty("Building_Configuration.Parameters_SP.fixtureStrategy_SP")]

...在每個{ get; set; } { get; set; } { get; set; }陳述? 我已經研究並閱讀了有關JsonPropertyJsonPropertyAttribute的信息,但我仍然不完全清楚。 我沒有看到該工具使用什么信號來生成屬性或它為什么這樣做。

該工具默認使用Json.net庫生成代碼,官方文檔對這個類沒有過多解釋: https ://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Serialization_JsonProperty.htm

關於如何使用它還有其他類似的問題,例如: What [JsonProperty] used in c#?

此類的一般用途是當您想要或需要重命名屬性時。 最后一個在這里是相關的。

json 解析器通常會嘗試將屬性名稱與您的類屬性進行 1:1 匹配。 但是每當 json 屬性名稱包含保留關鍵字、語言語法或其他非法屬性名稱時,您都需要對其進行重命名。

在您的示例中,名稱Building_Configuration.Parameters_SP.fixtureStrategy_SP包含句點。 如果您嘗試像這樣命名您的 get;set 屬性,則代碼將無法編譯。

生成代碼的站點知道這一點,並將添加所需的 JsonProperty 以將完整的 json 屬性名稱映射到已重命名為BuildingConfigurationParametersSPFixtureStrategySP的類字段(刪除了非法字符)

請參閱有效的屬性名稱: https ://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names

供參考: 訪問名稱中帶有點的屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM