Newtonsoft.Json用下划线将空格替换为JSON序列化的C#

问题描述:

初始问题:

这里有一个我用来将Entity Framework对象转换为JSON的函数:

Here I have a function I use to convert from Entity Framework object to JSON:

public class JSON
{
    public static string ConvertEntityToJSON(object dataToSerialize)
    {
        return JsonConvert.SerializeObject(dataToSerialize,
            new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            });
    }
}

调试时,"dataToSerialize"如下所示:

When debugging, "dataToSerialize" looks like following:

SELECT 
[xxx].[Firstattributefromdatabase] AS [First attribute from database], 
[xxx].[Secondattributefromdatabase] AS [Second attribute from database]
FROM [xxx]

可见,SQL的属性名称中单词之间有空格,而不是下划线,但输出(表中的一行)如下所示:

So as visible, there are spaces between words in the attribute names from SQL, not underscores, but the output (for one row in table) looks like following:

[{"First_attribute_from_database":"xxx","Second_attribute_from_database":"xxx"}]

任何可能导致此问题的线索以及如何解决?我将此JSON数据加载到数据透视表中,这导致所有字段都带有下划线而不是单词之间的空格.

Any clues for what might be causing this and how can this be fixed? I am loading this JSON data in a pivot table, which results in all fields having underscores instead of spaces between words.

更新1:

好的,因此,在查看了使用Entity Framework从数据库生成的内容(数据库的EF设计器;数据库优先的方法)之后,生成的类如下:

Okay, so after looking into what was generated from database using Entity Framework (EF designer from database; database-first approach), the class that was generated was the following:

public partial class TestClass
{
    public string First_attribute_from_database { get; set; }
    public string Second_attribute_from_database { get; set; }
}

即使在MS SQL Server Management Studio中明确定义了它是一个VIEW(而不是表本身),并且SQL的格式也如下:

Even though it was explicitly defined in the MS SQL Server Management Studio that this is a VIEW (not a table itself) and the SQL is formatted as following:

SELECT 
[xxx].[Firstattributefromdatabase] AS [First attribute from database], 
[xxx].[Secondattributefromdatabase] AS [Second attribute from database]
FROM [xxx]

所以这里出现一个问题:由于C#属性名称中不能有空格,如何解决下划线问题?

So here the question arises: How to overcome a problem of underlines as C# properties can't have spaces in their names?

已解决的问题:

与此同时发现了这篇精彩的文章.在此之后,我刚刚实现了该文章中的功能,并在ResolvePropertyName和voila中修改了Replace,我的代码正在出色地发挥作用!希望这对使用实体框架,从数据库生成模型并尝试从C#对象转换为JSON的其他所有人有所帮助.

Found this wonderful article in the meantime. Upon that I just implemented functions from the article, modified Replace in ResolvePropertyName and voila, my code is working splendidly! Hope this helps everyone else who is using Entity Framework, generating model from database and trying to go from C# objects to JSON.

public class JSON
{
    public static string ConvertEntityToJSON(object dataToSerialize)
    {
        return JsonConvert.SerializeObject(dataToSerialize,
            new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver = new UnderscorePropertyNamesContractResolver()
            });
    }
}

public class UnderscorePropertyNamesContractResolver : DefaultContractResolver
{
    public UnderscorePropertyNamesContractResolver() : base()
    {
    }

    protected override string ResolvePropertyName(string propertyName)
    {
        return Regex.Replace(propertyName, "_", " ");
    }
}