0

Is there a way in .NET that I can test if a JSON string I am about to send back to the client is valid? I am using an ASHX handler that accepts a bunch of form variables and returns a chunk of JSON that gets interpreted by the client. One of the parameters in the JSON contains a block of html that despite encoding sometimes gets corrupted with characters that cause a JSON error on the client. I'd like to catch this before it goes and email the sys admin with the details.

Additional info:

Target Framework 4.0

.ajax JQuery method used to send and read the response.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
QFDev
  • 8,668
  • 14
  • 58
  • 85

3 Answers3

1

Well, one idea is to try deserializing it server side (with your JSON framework of choice, be it Newtonsoft.Json or JsonFx) before sending it to the client.

Viktor Elofsson
  • 1,581
  • 2
  • 13
  • 20
0

I would use Json.NET and have something like JObject.Parse(json) in a try/catch block

snurre
  • 3,045
  • 2
  • 24
  • 31
0

You could try to deserialize it using the DataContractJsonSerializer class.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139