C# Json以一种特殊的方式连接到字典?
下面我有一些JSON数据,我想把它们转换成字典,现在我知道它不能简单地用一个简单的方法解析成字典,但我想知道如果我想这样做,我会从哪里开始 比如说,我想创建一个类似这样的字典C# Json以一种特殊的方式连接到字典?,c#,.net,json,C#,.net,Json,下面我有一些JSON数据,我想把它们转换成字典,现在我知道它不能简单地用一个简单的方法解析成字典,但我想知道如果我想这样做,我会从哪里开始 比如说,我想创建一个类似这样的字典 key=network.rcon.port, value=30001 key=network.rcon.allowed, value=127.0.0.1 key=network.rcon.ip_limit, value=5 由此 { "network": { "rcon": {
key=network.rcon.port, value=30001
key=network.rcon.allowed, value=127.0.0.1
key=network.rcon.ip_limit, value=5
由此
{
"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"
}
}
}
我将从Newtonsoft JSON.Net开始。然后可能创建一个C#类来反序列化JSON。差不多
public class Rcon {
public string port {get;set;}
public string allowed {get;set;}
public string ip_limit {get;set;}
}
您可能需要一个网络类和一个根类:
public class Network {
public Rcon rcon {get;set;}
}
public class MyRoot {
public Network network {get;set;}
}
然后,像这样反序列化:MyRoot=JsonConvert.DeserializeObject(“从任何地方获取的json字符串”)代码>
最后,将所需内容添加到词典中:
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);
var dict=newdictionary();
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);
可能有更复杂/通用的方法来实现这一点,但您需要深入研究。我将从Newtonsoft JSON.Net开始。然后可能创建一个C#类来反序列化JSON。差不多
public class Rcon {
public string port {get;set;}
public string allowed {get;set;}
public string ip_limit {get;set;}
}
您可能需要一个网络类和一个根类:
public class Network {
public Rcon rcon {get;set;}
}
public class MyRoot {
public Network network {get;set;}
}
然后,像这样反序列化:MyRoot=JsonConvert.DeserializeObject(“从任何地方获取的json字符串”)代码>
最后,将所需内容添加到词典中:
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);
var dict=newdictionary();
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);
可能有更复杂/通用的方法,但您需要深入研究。这里有一个更通用、更懒惰的解决方案:
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
};
}
}
}
公共静态类JsonExtensions
{
公共静态字典ToFlattenDictionary(此JToken标记,字符串路径=null)
{
开关(token.Type)
{
案例JTokenType.Object:
return-token.Children()
.SelectMany(x=>x.Value.ToFlattenDictionary(x.Name))
.ToDictionary(x=>path==null?x.Key:string.Join(“.”,path,x.Key),x=>x.Value);
案例JTokenType.Array:
返回令牌
.SelectMany((x,i)=>x.ToFlattenDictionary(i.ToString())
.ToDictionary(x=>path==null?x.Key:string.Join(“.”,path,x.Key),x=>x.Value);
违约:
返回新词典
{
[path]=(字符串)((JValue)令牌).Value
};
}
}
}
用法:JObject.Parse(json).ToFlattenDictionary()
下面是一个更通用、更懒惰的解决方案:
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
};
}
}
}
公共静态类JsonExtensions
{
公共静态字典ToFlattenDictionary(此JToken标记,字符串路径=null)
{
开关(token.Type)
{
案例JTokenType.Object:
return-token.Children()
.SelectMany(x=>x.Value.ToFlattenDictionary(x.Name))
.ToDictionary(x=>path==null?x.Key:string.Join(“.”,path,x.Key),x=>x.Value);
案例JTokenType.Array:
返回令牌
.SelectMany((x,i)=>x.ToFlattenDictionary(i.ToString())
.ToDictionary(x=>path==null?x.Key:string.Join(“.”,path,x.Key),x=>x.Value);
违约:
返回新词典
{
[path]=(字符串)((JValue)令牌).Value
};
}
}
}
用法:JObject.Parse(json).ToFlattenDictionary()
如果它是一种完全不同的语言,那么它是如何相同的?这两种方式都不适用于我的JSON分组。如果它是一种完全不同的语言,那么它是如何相同的?这两种方式都不适用于我的JSON分组。我宁愿不需要类,但是谢谢。@ropuxil如果你“宁愿”不想使用类,那么最好在别人浪费精力回答你的问题之前,把它作为你问题的一部分,谢谢!我宁愿这样做而不需要上课,但谢谢你。@ropuxil如果你“宁愿”不想使用课堂,那么最好在别人浪费精力回答你的问题之前,把它作为你问题的一部分陈述出来,谢谢!这真是太棒了,我觉得使用它有点便宜,但它仍然可以工作。谢谢。这太棒了,我觉得使用它有点便宜,但它仍然可以工作。谢谢。