C# - JSON Viewer

At some point you might have been exposed to JSON data whilst working on a project. Sometimes the data can be hard to read if your not using a decoder and your working with large data.
I developed this simple Windows application that might help you to visualize the data better. The code uses the System.Web.Extenions.dll. If you want to use it in a Windows Forms application, be sure to change the target framework from .Net Framework 4 Client Profile to .Net Framework 4 otherwise you wont be able to add the reference. Once you add the reference you'll have access to the System.Web.Script.Serialization namespace. Within this namespace is a class called JavaScriptSerializer. It has a Deserialize method which can be used to deserialize JSON data to a Type. Use can see from the example code how easy it is to deserialize JSON data with this class.
  1. JavaScriptSerializer js = new JavaScriptSerializer();
  2. try
  3. {
  4.         string strData = "JSON data";
  5.         Dictionary<string, object> dic = js.Deserialize<Dictionary<string, object>>(strData);
  6. }
  7. catch (ArgumentException argE)
  8. {
  9.         MessageBox.Show("JSON data is not valid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  10. }

Download

No comments:

Post a Comment