![]() Listing 1.2 below shows the complete BuildTree method. I gave a blue text color to each final TreeNode value to indicate it was not a parent TreeNode. Finally, if the value if not of either Dictionary or ArrayList Types, then I assume it is a string and create a final TreeNode. If the value is not of Type Dictionary, then I check if it is of Type ArrayList and if it is, then I loop through the items and add them as the final TreeNode. This method recursively loops through each item and checks if the value is of Type Dictionary and if it is, it passes the new Dictionary object to itself ( BuildTree) with a parent TreeNode. Notice in the code above, I have a method BuildTree(), which accepts a Dictionary object and a TreeNode. For this, I chose to use a TreeView control where a TreeNode could represent both a key and a value. Once I had deserialized the data, I needed a way to show it. MessageBox.Show( " JSON data is not valid") TreeNode rootNode = new TreeNode( " Root") JavaScriptSerializer js = new JavaScriptSerializer() ĭictionary dic = js.Deserialize>(txtInput.Text) Listing 1.1 below shows an excerpt of the deserializing process from the sample application. I used a Dictionary type since JSON can be considered to be a key/value pair where value can also be an array. It has a Deserialize method, which can deserialize the JSON data to a Type. Within this namespace is a class called JavaScriptSerializer. Now that I was able to add the reference, I could use the namespace. From this moment on, the head banging stopped. NET Framework 4 and checked the reference list again. After some time of head banging and frustration, I realized it was to do with the target framework. The Add Reference dialog box didn't list the reference. ![]() For some reason, I was not able to add a reference to. It's been a while since I worked on a Windows Forms application and immediately I ran into a problem. NET web application, I wondered how difficult it would be to develop a Windows application that could decode JSON data and present it to me in a more meaningful way. Normally, I would just write a simple script to make the data more readable but after working with the JavaScriptSerializer class in a recent. But recently, I was given a large JSON file which was initially an Ajax response from a website. Most of the time, I can look at the data and get a good understanding of what's going on, and if needed, I just run it through a JSON decode script usually in PHP. As an web developer, I'm almost always working with JSON data.
0 Comments
Leave a Reply. |