在开发过程中,往往会遇到数据类型转换的情况,根据自己的业务,可能转换类型有多种,下面就说一下json字符串和字典类型的转换。
public static class JsonUntity
{
/// <summary>
/// 将字典类型序列化为json字符串
/// </summary>
/// <typeparam name="TKey">字典key</typeparam>
/// <typeparam name="TValue">字典value</typeparam>
/// <param name="dict">要序列化的字典数据</param>
/// <returns>json字符串</returns>
public static string SerializeDictionaryToJsonString<TKey, TValue>(Dictionary<TKey, TValue> dict)
{
if (dict.Count == )
return ""; string jsonStr = JsonConvert.SerializeObject(dict);
return jsonStr;
} /// <summary>
/// 将json字符串反序列化为字典类型
/// </summary>
/// <typeparam name="TKey">字典key</typeparam>
/// <typeparam name="TValue">字典value</typeparam>
/// <param name="jsonStr">json字符串</param>
/// <returns>字典数据</returns>
public static Dictionary<TKey, TValue> DeserializeStringToDictionary<TKey, TValue>(string jsonStr)
{
if (string.IsNullOrEmpty(jsonStr))
return new Dictionary<TKey, TValue>(); Dictionary<TKey, TValue> jsonDict = JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(jsonStr); return jsonDict; }
}