【发布时间】:2016-10-08 04:10:35
【问题描述】:
我有两种方法
public static string SerializeObject<T>(T value)
{
if (value == null)
{
return null;
}
var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
var jsonString = JsonConvert.SerializeObject(dictionaryObject);
return jsonString;
}
和
public static T DeserializeObject<T>(string jsonString)
{
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString());
}
当我反序列化一个带有类型的 json 字符串时
ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>
我有一个例外:
无法将字符串“[1, 1]”转换为字典键类型“System.Collections.Generic.KeyValuePair`2[System.Int64,System.Int64]”。创建一个 TypeConverter 以从字符串转换为键类型对象。路径“[1, 1]”,第 2 行,位置 12。
那么有人可以告诉我正确的代码吗?
这是我的代码:
var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });
var se = SerializeObject(test);
var de = DeserializeObject<ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>>(se);
【问题讨论】:
-
你为什么要把它变成字典?
-
您可能需要多解释一下您要完成的工作。不确定您要使用 Dictionary 解决什么问题。
-
@ user1334007:我正在尝试将 json 字符串反序列化为 ConcurrentDictionary
, IList 类型的对象> -
只删除字典的东西,没有意义。另外你有一些相当奇怪的数据结构,也许可以尝试使用一个类作为 DTO(数据传输对象),它会简化你的代码并使其更符合逻辑
标签: c# asp.net json deserialization json-deserialization