Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a string JSON Validator [duplicate]

Tags:

json

c#

.net

I'm using newtonsoft's JSON.Net and loving every minute of it.

However, I am using JObject.Parse(jsonString) to grab a JToken from a response string. If I send invalid JSON, I get an exception. I can catch the exception, but I would like to, be able to test the string first before sending it to Parse.

Maybe something akin to JObject.TryParse() (which doesn't exist).

I'd even take bool ValidJson(string)

I know there's JSONLint out there, but I would really like to keep the external calls to a minimum.

Any ideas?

like image 507
Rob Avatar asked Aug 18 '26 23:08

Rob


1 Answers

The simplest solution would be to write a function which calls JObject.Parse and returns false if it throws a Newtonsoft.Json.JsonReaderException.

like image 168
MRAB Avatar answered Aug 20 '26 13:08

MRAB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!