json属性为小写c#

时间:2018-02-01 08:15:09

标签: c# json

我从客户端json字符串获取:

{ "Client": { "Name": "John" } }

但是为了进一步处理,我需要以下json:

{ "client": { "name": "John" } }

我尝试了类似的东西,但它没有帮助:

public class LowerCaseNamingStrategy : NamingStrategy
{
    protected override string ResolvePropertyName(string name)
    {
        return name.ToLower();
    }
}

var settings = new JsonSerializerSettings();
settings.ContractResolver = new DefaultContractResolver { NamingStrategy = new LowerCaseNamingStrategy() };
var json = JsonConvert.DeserializeObject(input.DataJson, settings);

JSON是动态对象,因此我不知道属性存在。 我怎么能用c#做到这一点?使用Newtonsoft.Json或使用Xml。

5 个答案:

答案 0 :(得分:1)

尝试使用LowercaseContractResolver

var settings = new JsonSerializerSettings();
settings.ContractResolver = new LowercaseContractResolver();
var json = JsonConvert.DeserializeObject(input.DataJson, settings);

答案 1 :(得分:1)

如果我理解正确,您需要修改Json字符串中的属性,但不要将Json转换为对象。

在这种情况下,您可以尝试将Json解析为JObject并替换该对象中的属性。

    private static void ChangePropertiesToLowerCase(JObject jsonObject)
    {
        foreach (var property in jsonObject.Properties().ToList())
        {
            if(property.Value.Type == JTokenType.Object)// replace property names in child object
                ChangePropertiesToLowerCase((JObject)property.Value);

            property.Replace(new JProperty(property.Name.ToLower(),property.Value));// properties are read-only, so we have to replace them
        }
    }

样品:

var jsonString = @"{ ""Client"": { ""Name"": ""John"" } }";
var jobj = JObject.Parse(jsonString, new JsonLoadSettings());
ChangePropertiesToLowerCase(jobj);

var stringWithLowerCaseProperties = jobj.ToString(Formatting.None);

答案 2 :(得分:0)

这是没有正则表达式的简单方法。将每个[{" A]替换为[{" a]

var json = "{ \"Client\": { \"Name\": \"John\" } }";
var newJson = string.Empty;

foreach (var w in json.Split(new[] { "{ \"" }, StringSplitOptions.RemoveEmptyEntries))
{
    if (w[0] != null)
    {
        newJson += "{ \"" + (w[0].ToString().ToLower()) + w.Remove(0,1);
    }
}

结果:

"{ \"client\": { \"name\": \"John\" } }"

答案 3 :(得分:0)

扩展JSON可以包含对象数组的情况的Anton Semenov答案:

private static void ChangePropertiesToLowerCase(JObject jsonObject)
{
    foreach (var property in jsonObject.Properties().ToList())
    {
        if (property.Value.Type == JTokenType.Object) // replace property names in child object
            ChangePropertiesToLowerCase((JObject)property.Value);

        if (property.Value.Type == JTokenType.Array)
        {
            var arr = JArray.Parse(property.Value.ToString());
            foreach (var pr in arr)
            {
                ChangePropertiesToLowerCase((JObject)pr);
            }

            property.Value = arr;
        }

        property.Replace(new JProperty(property.Name.ToLower(CultureInfo.InvariantCulture), property.Value)); // properties are read-only, so we have to replace them
    }
}

答案 4 :(得分:0)

此处所有其他解决方案都将修改原始对象。这是一个不可变的版本,它返回具有小写字母属性的新对象:

public static class JsonExtensions
{
    public static JToken ToLowerRecursive(this JToken token)
    {
        if (token is JObject jobj)
            return new JObject(jobj.Properties().Select(x => new JProperty(x.Name.ToLowerInvariant(), x.Value.ToLowerRecursive())));

        if (token is JArray jarr)
            return new JArray(jarr.Select(x => x.ToLowerRecursive()));

        return token;
    }
}