将JSON解析为JToken时如何将所有键更改为小写

将JSON解析为JToken时如何将所有键更改为小写

本文介绍了将JSON解析为JToken时如何将所有键更改为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,并且键具有大写和小写字符:

I have a string of JSON and the keys have uppercase and lowercase characters:

{"employees":[
    {"FIrstName":"John", "LASTname":"Doe"},
    {"FIRSTNAME":"Anna", "LaSTNaME":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

我想将其转换为JToken对象,并将JToken中的所有键都转换为小写.因此,在JToken内部,它应该如下所示:

I want convert it to a JToken object and have all the keys in the JToken be lowercase. So internally in the JToken it should be as follows:

{"employees":[
    {"firstname":"John", "lastname":"Doe"},
    {"firstname":"Anna", "lastname":"Smith"},
    {"firstname":"Peter", "lastname":"Jones"}
]}

以前,我是使用JToken json = JToken.Parse(jsonString);进行转换的,但是我找不到如何使键变为小写的方式. 有什么想法吗?

Previously I was using JToken json = JToken.Parse(jsonString); to convert, but I can't find out how to make the keys lowercase. Any ideas?

之所以需要这样做是因为我的JsonSchema验证不区分大小写.

The reason why I need to do this is so that my JsonSchema validation will be case insensitive.

推荐答案

使用最少的代码即可解决此问题的一种可能方法是,将JsonTextReader子类化并覆盖Value属性,以便在当前是PropertyName:

One possible way to solve this with minimal code is to subclass the JsonTextReader and override the Value property to return a lowercase string whenever the current TokenType is PropertyName:

public class LowerCasePropertyNameJsonReader : JsonTextReader
{
    public LowerCasePropertyNameJsonReader(TextReader textReader)
        : base(textReader)
    {
    }

    public override object Value
    {
        get
        {
            if (TokenType == JsonToken.PropertyName)
                return ((string)base.Value).ToLower();

            return base.Value;
        }
    }
}

之所以有效,是因为基础JsonTextReader会随着内部状态的变化而使TokenType保持更新,并且序列化程序(实际上是JsonSerializerInternalReader类)依赖于该序列化程序,以便通过以下方式从读取器中检索属性名称: Value属性.

This works because the underlying JsonTextReader keeps the TokenType updated as its internal state changes, and the serializer (actually the JsonSerializerInternalReader class) relies on that when it goes to retrieve the property name from the reader via the Value property.

您可以创建一个简短的辅助方法,以便使用自定义阅读器轻松进行反序列化:

You can create a short helper method to make it easy to deserialize using the custom reader:

public static class JsonHelper
{
    public static JToken DeserializeWithLowerCasePropertyNames(string json)
    {
        using (TextReader textReader = new StringReader(json))
        using (JsonReader jsonReader = new LowerCasePropertyNameJsonReader(textReader))
        {
            JsonSerializer ser = new JsonSerializer();
            return ser.Deserialize<JToken>(jsonReader);
        }
    }
}

然后在您的代码中,只需将其替换:

Then in your code, just replace this:

JToken json = JToken.Parse(jsonString);

与此:

JToken json = JsonHelper.DeserializeWithLowerCasePropertyNames(jsonString);

提琴: https://dotnetfiddle.net/A0S3I1

这篇关于将JSON解析为JToken时如何将所有键更改为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:23