验证字符串是否为.NET Core 3.0中的有效json(可能的最快方式) [英] Validate if string is valid json (fastest way possible) in .NET Core 3.0

查看:37
本文介绍了验证字符串是否为.NET Core 3.0中的有效json(可能的最快方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用带有try/catch的外部库(newtonsoft)来检查字符串是否为有效的json结构.我不想反序列化到一个对象(因为json可以是一个或多个属性),只是要确保其有效的json.

I know i can use an external library(newtonsoft) with a try/catch to check if a string is valid json structure. I do not want to deserialize to an object (since the json can be one or many properties) point is to just make sure its valid json.

我宁愿使用System.Text.Json,但不确定哪种最好,TryParseValue,JsonDocument等

I would prefer to use the System.Text.Json but not sure what would be the best, TryParseValue, JsonDocument, etc

推荐答案

我使用Utf8JsonReader并只需在根对象上调用TrySkip()即可;这将检查它是否至少格式正确.

I use Utf8JsonReader and simply call TrySkip() on the root object; this will check that it's at least well formed.

这篇关于验证字符串是否为.NET Core 3.0中的有效json(可能的最快方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆