【发布时间】:2018-02-18 04:36:57
【问题描述】:
我在将 JSON 转换为字典时遇到问题,谁能帮忙?
04:33:25 - 加载配置时出错。 System.InvalidCastException: 无法将“System.Int64”类型的对象转换为“System.String”类型。 在 Seegal.Core.Extentions.JsonExtensions.ToFlatDictionary(JToken 令牌,字符串路径)在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 28 在 Seegal.Core.Extentions.JsonExtensions.c.b__0_0(JProperty x) 在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 19 在 System.Linq.Enumerable.d__17
2.MoveNext() at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector)在 Seegal.Core.Extentions.JsonExtensions.ToFlatDictionary(JToken token, 字符串路径)在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 18 点 Seegal.Core.Extentions.JsonExtensions.c.b__0_0(JProperty x) 在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 19 在 System.Linq.Enumerable.d__172.MoveNext() at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector)在 Seegal.Core.Extentions.JsonExtensions.ToFlatDictionary(JToken token, 字符串路径)在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 18 点 Seegal.Core.Extentions.JsonExtensions.c.b__0_0(JProperty x) 在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 19 在 System.Linq.Enumerable.d__172.MoveNext() at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector)在 Seegal.Core.Extentions.JsonExtensions.ToFlatDictionary(JToken token, 字符串路径)在 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Extentions\JsonExtentions.cs:line 18 在 Seegal.Core.Config.ConfigHandler.Load(String apiUrl) 中 C:\Users\admin\workspace\Seegal\Seegal\Seegal\Core\Config\ConfigHandler.cs:line 49
我要转换成字典的 JSON:
{
"bingo": {
"ftp" : {
"host" : "",
"port" : 21,
"username" : "",
"password" : "",
"enabled" : 0,
}
},
"snowman": {
"sockets" : {
"host" : "127.0.0.1",
"port" : 2000,
}
}
}
方法:
public static class JsonExtensions
{
public static Dictionary<string, string> ToFlatDictionary(this JToken token, string path = null)
{
switch (token.Type)
{
case JTokenType.Object:
return token.Children<JProperty>()
.SelectMany(x => x.Value.ToFlatDictionary(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.ToFlatDictionary(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
};
}
}
}
转换行,发生错误的行:
_configElements = JObject.Parse(responseText).ToFlatDictionary();
【问题讨论】: