Newtonsoft.Json replacing spaces with underscores C# to JSON Serialize [duplicate]

情到浓时终转凉″ 提交于 2019-12-22 13:45:46

问题


INITIAL QUESTION:

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
            });
    }
}

When debugging, "dataToSerialize" looks like following:

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

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"}]

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.


UPDATE 1:

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; }
}

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]

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


回答1:


PROBLEM SOLVED:

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, "_", " ");
    }
}


来源:https://stackoverflow.com/questions/47589794/newtonsoft-json-replacing-spaces-with-underscores-c-sharp-to-json-serialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!