ropuxil
ropuxil

Reputation: 131

Json to dictionary in a particular way?

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

Upvotes: 0

Views: 68

Answers (2)

Xiaoy312
Xiaoy312

Reputation: 14477

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

Upvotes: 2

Matthew Groves
Matthew Groves

Reputation: 26169

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.

Upvotes: 0

Related Questions