Computer Beacon  

Creating JsonObjects in C#

Posted on 1/15/2012

In this blog post I'll talk more about the JSON Toolkit. JSON encoded strings are widely used on the internet for data transmission or storage. Version 2.0 of JSON Toolkit adds features including creating and manipulating JSON objects. But first, let's talk about parsing JSON strings.

Parsing JSON strings

Let's say I have a text file with the following content:

{ "Image": { "Width": 800, "Height": 600, "Title": "View from 15th Floor", "Thumbnail": { "Url": "http://www.example.com/image/481989943", "Height": 125, "Width": 100 }, "IDs": [116, 943, 234, 38793] } }

It's easy to convert the above string into an instance of JsonObject by calling its constructor.

JsonObject JO = new JsonObject(File.ReadAllText(@"D:\Some Path Here\Sample.txt"));

The string can come from anywhere, such as text files, web services (as is the case in Facebook Graph Toolkit), or even user input. As long as it is a valid JSON string, the library can parse it. Note that the JSON Toolkit does not support number values using the "e" notation at this point.

Creating JSON objects

It is also possible to add key-value pairs to a JSON object starting with an empty one. The code below shows how to create a JSON object with content almost identical to the one above.

JsonObject ImageJO = new JsonObject();
ImageJO.Add("Width", 800);
ImageJO.Add("Height", 600);
ImageJO.Add("Title", "View from 15th floor");

JsonObject ThumbJO = new JsonObject();
ThumbJO.Add("Url", "http://www.example.com/image/481989943");
ThumbJO.Add("Height", 125);
ThumbJO.Add("Width", 100);
ImageJO.Add("Thumbnail", ThumbJO); //Nested JsonObject here

JsonArray JA = new JsonArray();
JA.Add(116);
JA.Add(943);
JA.Add(234);
JA.Add(38793);
ImageJO.Add("IDs", JA); //Nested JsonArray here

Each "property" in a JsonObject is associated with a key, so the keys must be unique. The type of each property can be one of the followings: string, any number types in C#, bool, a JsonArray or JsonObject. Elements in JsonObject are unordered and are only identified by keys. Elements in JsonArray however are ordered.

The above code almost produces a JsonObject same as the parse example. The only step missing is to put the image object inside another JsonObject.

JsonObject JO = new JsonObject();
JO.Add("Image", ImageJO);

Output a JsonObject

To output a JsonObject (e.g. save it in a text file or database), simply call its ToString() method.

The output string is a string identical to the sample above, but without all the unnecessary white spaces. When parsing, the JSON Toolkit accepts an arbitary number of white spaces (as well as new lines) between JSON syntax characters. The ToString() method always produce the shortest string that represents the data, thus there will be no white spaces.

The string can be later retrieved to create another instance of JsonObject at runtime which represents identical data. The following code thus duplicates an instance of JsonObject by creating another instance with the exact same data:

JsonObject SecondJO = new JsonObject(FirstJO.ToString());


You must be logged in through Facebook to write comments.

Comments

There are no comments at the moment