System.Text.Json
在.NET Core 3.0中加入了对JSON的内置支持。
序列化
using System.Text.Json;
using System.Text.Json.Serialization;
JsonSerializer.Serialize(DemoEntity)
读取与写入
// 读取
var str = File.ReadAllText(file);
var data = JsonSerializer.DeserializeObject<List<Demo>>(str);
// 写入
str = JsonSerializer.Serialize(DemoEntity);
File.WriteAllText(file, str);
时间格式化
/// <summary>
/// 自定义时间转化器
/// </summary>
public class DateTimeConverterUsingDateTimeParse : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyyMMddHHmmss"));
}
}
JsonSerializerOptions options = new JsonSerializerOptions();
options.Converters.Add(new DateTimeConverterUsingDateTimeParse());
string jsonString = JsonSerializer.Serialize(data, options);
序列化扩展
/// <summary>
/// 小驼峰格式反序列化。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <returns></returns>
public static T DeserializeWithCamel<T>(string str)
{
if (str != null)
{
// 小驼峰序列化
var setting = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return JsonSerializer.Deserialize<T>(str, setting);
// Newtonsoft
var setting = new JsonSerializerSettings
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
};
return JsonConvert.DeserializeObject<T>(str, setting);
}
else
{
return default(T);
}
}
/// <summary>
/// 小驼峰格式序列化。
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="entity">类型对象</param>
/// <returns></returns>
public static string SerializeWithCamel<T>(T entity)
{
// 小驼峰序列化
var setting = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
return JsonSerializer.Serialize(entity, setting);
// Newtonsoft
var setting = new JsonSerializerSettings
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
};
return JsonConvert.SerializeObject(entity, setting);
}
格式化json字符串
.NET Core版本
/// <summary>
/// 格式化JSON字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ConvertJsonString<T>(T obj)
{
if (obj != null)
{
var setting = new JsonSerializerOptions
{
WriteIndented = true,
};
return JsonSerializer.Serialize(obj, setting);
}
return string.Empty;
}
Newtonsoft版本
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
/// <summary>
/// 格式化JSON字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">类型对象</param>
/// <returns></returns>
public static string ConvertJsonString<T>(T obj)
{
if (obj != null)
{
JsonSerializer serializer = new JsonSerializer();
StringWriter writer = new StringWriter();
JsonTextWriter jwriter = new JsonTextWriter(writer)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' ',
};
serializer.Serialize(jwriter, obj);
return writer.ToString();
}
return string.Empty;
}
若是序列化后的字符串,不知道相应的对象类型,可以如下转换:
/// <summary>
/// 格式化JSON字符串
/// </summary>
/// <param name="string">序列化后的字符串</param>
/// <returns></returns>
public static string ConvertJsonString(string str)
{
try
{
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if (obj != null)
{
StringWriter writer = new StringWriter();
JsonTextWriter jwriter = new JsonTextWriter(writer)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' ',
};
serializer.Serialize(jwriter, obj);
return writer.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return str;
}
反序列化
Json
Newtonsoft
public static T JsonToObject<T>(string jsonStr)
{
return JsonConvert.DeserializeObject<T>(jsonStr);
}