如何解决如何使用 System.Text.Json 对未知对象进行漂亮的打印
使用 System.Text.Json 我可以使用序列化选项漂亮地打印 json。
thread '<unnamed>' panicked at 'there is no timer running,must be called from the context of Tokio runtime',/home/admin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.0.1/src/time/driver/handle.rs:50:18
但是,我有字符串 JSON 并且不知道 concreate 类型。我如何漂亮地打印 JSON 字符串?
我的旧代码使用 Newtonsoft,无需序列化/反序列化即可完成
var options = new JsonSerializerOptions{ WriteIndented = true };
jsonString = JsonSerializer.Serialize(typeToSerialize,options);
解决方法
这有效:
using System.Text.Json;
public static string JsonPrettify(this string json)
{
var jDoc = JsonDocument.Parse(json);
return JsonSerializer.Serialize(jDoc,new JsonSerializerOptions { WriteIndented = true });
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。