更新时间:2023-11-26 20:52:16
这个例子在我使用 .NET Core 3.1 时对我有用,如果字符串不是格式正确的 JSON 会抛出异常:
This example has worked for me using .NET Core 3.1 and will throw an exception if a string is not well-formed JSON:
string json = "{ "TestKey": "TestValue" }";
// Ensure the string is valid JSON.
try
{
var js = new Newtonsoft.Json.JsonSerializer();
js.Deserialize(new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(json)));
}
catch (Newtonsoft.Json.JsonReaderException)
{
throw;
}