0

I have some JSON data below that I want to turn into a dictionary, now I know it can't simply be parsed into a dictionary with a simple method but I want to know where I would start if I wanted to do it like this?

So for example, lets say I wanted to create a dictionary something like this...

key=network.rcon.port, value=30001
key=network.rcon.allowed, value=127.0.0.1
key=network.rcon.ip_limit, value=5

From this

{
    "network": {
        "rcon": {
            "port": "30001",
            "allowed": "127.0.0.1",
            "limit": "100",
            "ip_limit": "5"
        },

        "sockets": {
            "port": "30000",
            "backlog": "500",
            "no_delay": "1"
        }
    },

    "game": {
        "players": {
            "limit": "10000",
            "ip_limit": "4"
        }
    }
}
ropuxil
  • 121
  • 2
  • 12
  • Possible duplicate of [How can I deserialize JSON to a simple Dictionary in ASP.NET?](https://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net) – string.Empty Jan 24 '18 at 22:16
  • How is that the same when its a totally different language? – ropuxil Jan 24 '18 at 22:43
  • This won't work either way as my JSON is grouped. – ropuxil Jan 24 '18 at 22:44

2 Answers2

2

Here is a more generic and lazy solution:

public static class JsonExtensions
{
    public static Dictionary<string, string> ToFlattenDictionary(this JToken token, string path = null)
    {
        switch (token.Type)
        {
            case JTokenType.Object:
                return token.Children<JProperty>()
                    .SelectMany(x => x.Value.ToFlattenDictionary(x.Name))
                    .ToDictionary(x => path == null ? x.Key : string.Join(".", path, x.Key), x => x.Value);

            case JTokenType.Array:
                return token
                    .SelectMany((x, i) => x.ToFlattenDictionary(i.ToString()))
                    .ToDictionary(x => path == null ? x.Key : string.Join(".", path, x.Key), x => x.Value);

            default:
                return new Dictionary<string, string>
                {
                    [path] = (string)((JValue)token).Value
                };
        }
    }
}

Usage: JObject.Parse(json).ToFlattenDictionary()

Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
0

I would start with Newtonsoft JSON.Net. Then maybe create a C# class to deseralize the JSON into. Something like

public class Rcon {
    public string port {get;set;}
    public string allowed {get;set;}
    public string ip_limit {get;set;}
}

And you'll probably need a network class and a class for the root:

public class Network {
    public Rcon rcon {get;set;}
}

public class MyRoot {
    public Network network {get;set;}
}

Then, deserialize like so: MyRoot root = JsonConvert.DeserializeObject<MyRoot>("your json string from wherever you are getting it");

Finally, add what you want to the dictionary:

var dict = new Dictionary<string,string>();
dict.Add("network.rcon.port", root.network.rcon.port);
dict.Add("network.rcon.allowed", root.network.rcon.allowed);
dict.Add("network.rcon.ip_limit", root.network.rcon.ip_limit);

There are probably more sophisticated/generic ways to do it, but you'll need to dig into the documentation.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • I would rather do it without the need for classes, but thanks for this. – ropuxil Jan 24 '18 at 22:35
  • @ropuxil if you would "rather" not want to use classes - then better state it as part of your question before someone else wastes their efforts in trying to answer your question, thanks! – Jaya Jan 24 '18 at 22:41