public static bool IsJsonString(string str)
{
if (string.IsNullOrWhiteSpace(str)) { return false; }
str = str.Trim();
if ((str.StartsWith("{") && str.EndsWith("}")) ||
(str.StartsWith("[") && str.EndsWith("]")))
{
try
{
var obj = JToken.Parse(str);
return true;
}
catch (JsonReaderException jex)
{
return false;
}
catch (Exception ex) //some other exception
{
return false;
}
}
else
{
return false;
}
}
Check if the string is valid JSON in C#
C# validate json string