Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

c# - How to change all keys to lowercase when parsing JSON to a JToken

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

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

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

with this:

JToken json = JsonHelper.DeserializeWithLowerCasePropertyNames(jsonString);

Fiddle: https://dotnetfiddle.net/A0S3I1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...