1private static bool IsValidJson(string strInput)
2{
3 if (string.IsNullOrWhiteSpace(strInput)) { return false;}
4 strInput = strInput.Trim();
5 if ((strInput.StartsWith("{") && strInput.EndsWith("}")) || //For object
6 (strInput.StartsWith("[") && strInput.EndsWith("]"))) //For array
7 {
8 try
9 {
10 var obj = JToken.Parse(strInput);
11 return true;
12 }
13 catch (JsonReaderException jex)
14 {
15 //Exception in parsing json
16 Console.WriteLine(jex.Message);
17 return false;
18 }
19 catch (Exception ex) //some other exception
20 {
21 Console.WriteLine(ex.ToString());
22 return false;
23 }
24 }
25 else
26 {
27 return false;
28 }
29}