You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将含带点键名的JSON转换为合法C#对象?

Fixing Invalid C# Class Names from JSON with Dot-Separated Keys

Hey there! I totally get the frustration—auto-generators like json2csharp struggle with JSON keys that have dots (like krs_podmioty.data_sprawdzenia) because C# doesn't allow dots in property names. Those messy __invalid_name__ placeholders are useless for actual code, so let's fix this properly.

The Root Problem

JSON keys with dots aren't valid C# identifiers, so the auto-generator can't create a proper property name. Instead of relying on the tool, we'll manually map those keys to valid C# property names using JSON serialization attributes.

Solution 1: Using Newtonsoft.Json (Most Common)

If you're using Newtonsoft.Json (the popular Json.NET library), we'll use the [JsonProperty] attribute to link each valid C# property name to its original JSON key. Here's the corrected class structure:

using Newtonsoft.Json;
using System;

public class RootObject
{
    [JsonProperty("data")]
    public DataContainer Data { get; set; }
}

public class DataContainer
{
    [JsonProperty("krs_podmioty.data_sprawdzenia")]
    public DateTime DataSprawdzenia { get; set; }

    [JsonProperty("krs_podmioty.regon")]
    public string Regon { get; set; }

    [JsonProperty("krs_podmioty.adres_lokal")]
    public string AdresLokal { get; set; }

    [JsonProperty("krs_podmioty.adres_miejscowosc")]
    public string AdresMiejscowosc { get; set; }

    [JsonProperty("krs_podmioty.liczba_czlonkow_komitetu_zal")]
    public int LiczbaCzlonkowKomitetuZal { get; set; }
}

To deserialize your JSON string, you'd do:

var json = "{ \"data\": { \"krs_podmioty.data_sprawdzenia\": \"2016-12-22T05:36:21\", \"krs_podmioty.regon\": \"0\", \"krs_podmioty.adres_lokal\": \"\", \"krs_podmioty.adres_miejscowosc\": \"Warszawa\", \"krs_podmioty.liczba_czlonkow_komitetu_zal\": 0 } }";
var result = JsonConvert.DeserializeObject<RootObject>(json);

Solution 2: Using System.Text.Json (.NET Core/.NET 5+)

If you're working with the built-in System.Text.Json library (no external dependencies), use the [JsonPropertyName] attribute instead:

using System;
using System.Text.Json.Serialization;

public class RootObject
{
    [JsonPropertyName("data")]
    public DataContainer Data { get; set; }
}

public class DataContainer
{
    [JsonPropertyName("krs_podmioty.data_sprawdzenia")]
    public DateTime DataSprawdzenia { get; set; }

    [JsonPropertyName("krs_podmioty.regon")]
    public string Regon { get; set; }

    [JsonPropertyName("krs_podmioty.adres_lokal")]
    public string AdresLokal { get; set; }

    [JsonPropertyName("krs_podmioty.adres_miejscowosc")]
    public string AdresMiejscowosc { get; set; }

    [JsonPropertyName("krs_podmioty.liczba_czlonkow_komitetu_zal")]
    public int LiczbaCzlonkowKomitetuZal { get; set; }
}

Deserialization here looks like:

var result = System.Text.Json.JsonSerializer.Deserialize<RootObject>(json);

This approach keeps your C# code clean and compliant, while still correctly mapping to the original JSON structure.

内容的提问来源于stack exchange,提问作者Adriano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:39:32