1

I'd like to parse from the website: http://ddragon.leagueoflegends.com/cdn/10.9.1/data/en_US/champion.json
Use this website for better view. When I type Aatrox I would like to retrieve 266

I managed to do it so far but it is case sensitive. Is there any way to get my code be case-insensitive?

var input = new WebClient().DownloadString(@"http://ddragon.leagueoflegends.com/cdn/10.9.1/data/en_US/champion.json");
var obj = JObject.Parse(input);
var input= obj["data"]["Aatrox"]["key"];


Updated Code

var input = new WebClient().DownloadString(@"http://ddragon.leagueoflegends.com/cdn/10.9.1/data/en_US/champion.json");
JObject json = (JObject)JsonConvert.DeserializeObject(input);
Dictionary<string, object> d = new Dictionary<string, object>(json.ToObject<IDictionary<string, object>>(), StringComparer.CurrentCultureIgnoreCase);
String f = d["data"]["Aatrox"]["key"].ToString();

It is still not working.

XHSKR
  • 13
  • 3
  • Does this answer your question? [JSON.NET JObject key comparison case-insensitive](https://stackoverflow.com/questions/12055743/json-net-jobject-key-comparison-case-insensitive) – Pavel Anikhouski Apr 30 '20 at 12:20

1 Answers1

0

I think that you can use Dictionary instead of anonymous object, see this answer

Update:

    public class InsensitiveWrapper
    {
        private readonly JObject _rWrapped;
        private readonly string _rLeafValue;

        public InsensitiveWrapper(JObject jsonObj)
        {
            _rWrapped = jsonObj ?? throw new ArgumentNullException(nameof(jsonObj));
        }

        private InsensitiveWrapper(string value)
        {
            _rLeafValue = value ?? throw new ArgumentNullException(nameof(value));
        }

        public string Value => _rLeafValue ?? throw new InvalidOperationException("Value can be retrieved only from leaf.");

        public InsensitiveWrapper this[string key]
        {
            get
            {
                object nonTyped = _rWrapped.GetValue(key, StringComparison.OrdinalIgnoreCase);
                if (nonTyped == null)
                    throw new KeyNotFoundException($"Key {key} is not found.");


                JObject jObject = nonTyped as JObject;
                if (jObject == null)
                    return new InsensitiveWrapper(nonTyped.ToString());

                return new InsensitiveWrapper(jObject);
            }
        }
    }

    public static async Task Main()
    {
        var input = new WebClient().DownloadString(@"http://ddragon.leagueoflegends.com/cdn/10.9.1/data/en_US/champion.json");
        JObject json = (JObject)JsonConvert.DeserializeObject(input);

        var dictionary = new InsensitiveWrapper(json);

        var val = dictionary["data"]["Aatrox"]["key"].Value;

        Console.WriteLine(val);
    }
  • I tried using dictionary but it seems not work on multiple curly brackets – XHSKR Apr 30 '20 at 12:42
  • Thanks !! It works now as it should. It's way difficult than I thought haha. I'd rather copy & paste instead of understanding the code. Thank you again for your help. – XHSKR Apr 30 '20 at 13:34