反序列化时出现异常 json
Exception when deserializing json
我有两种方法
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);
我不确定这是否是最佳解决方案,但是,请试试这个:
1) 按照 this topic.
中的描述创建 ContractResolver
class DictionaryAsArrayResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (objectType.GetInterfaces().Any(i => i == typeof(IDictionary) ||
(i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IDictionary<,>))))
{
return base.CreateArrayContract(objectType);
}
return base.CreateContract(objectType);
}
}
2) 稍微改变一下您的 Serialize/Deserialize 方法:
public static string SerializeObject<T>(T value, JsonSerializerSettings settings)
{
if (value == null)
{
return null;
}
var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
var jsonString = JsonConvert.SerializeObject(dictionaryObject, settings);
return jsonString;
}
public static T DeserializeObject<T>(string jsonString, JsonSerializerSettings settings)
{
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString, settings);
return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString(), settings);
}
3) 检查测试:
[TestMethod]
public void Test()
{
var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new DictionaryAsArrayResolver();
var se = SerializeObject(test, settings);
var de = DeserializeObject<ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>>(se, settings);
}
希望对你有所帮助=)
我有两种方法
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);
我不确定这是否是最佳解决方案,但是,请试试这个:
1) 按照 this topic.
中的描述创建 ContractResolverclass DictionaryAsArrayResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (objectType.GetInterfaces().Any(i => i == typeof(IDictionary) ||
(i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IDictionary<,>))))
{
return base.CreateArrayContract(objectType);
}
return base.CreateContract(objectType);
}
}
2) 稍微改变一下您的 Serialize/Deserialize 方法:
public static string SerializeObject<T>(T value, JsonSerializerSettings settings)
{
if (value == null)
{
return null;
}
var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
var jsonString = JsonConvert.SerializeObject(dictionaryObject, settings);
return jsonString;
}
public static T DeserializeObject<T>(string jsonString, JsonSerializerSettings settings)
{
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString, settings);
return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString(), settings);
}
3) 检查测试:
[TestMethod]
public void Test()
{
var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new DictionaryAsArrayResolver();
var se = SerializeObject(test, settings);
var de = DeserializeObject<ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>>(se, settings);
}
希望对你有所帮助=)