Working with JSON in .NET – a Silverlight example
Join the DZone community and get the full member experience.
Join For FreeUnlike any other .NET sub-framework, Silverlight has built capabilities to work with JSON. In this article, I am focusing on JsonObject, a class that can help the developer work with JSON content.
Here is my sample JSON data representation:
{ "parts" : {Nothing complicated to this point. Now, I want to read the array of objects declared inside the part element. To do this, I am going to declare an instance of JsonObject and JsonArray. While JsonObject handles single objects, an array will contain a multitude of objects.
"part" : [
{ "name" : "Mainboard" },
{ "name" : "Keyboard" },
{ "name" : "CPU" },
{ "name"" : "FAN" }
] }
}
An important note to be made is that you need to reference the System.Json library in order to work with the above mentioned classes.
Initially, I stored the existing JSON code in a sample string:
string jsonString = @"{ ""parts"" : {
""part"" : [
{ ""name"" : ""Mainboard"" },
{ ""name"" : ""Keyboard"" },
{ ""name"" : ""CPU"" },
{ ""name"" : ""FAN"" }
] }
}";
Now, if one quite simple task would be obtaining the names of each part present in the array. To do this, I am going to create an instance of JsonObject and pass the JSON string to the Parse method for the same class:
JsonObject jsonDoc = (JsonObject)JsonObject.Parse(jsonString);
Now, I need to actually handle the existing data array. To do this, I am instantiating JsonArray and show the path to the needed array via object names:
JsonArray jsonArray = (JsonArray)jsonDoc["parts"]["part"];
This array contains all elements inside the part set. To get the actual names, I am using this code:
foreach (JsonObject obj in jsonArray)
{
JsonValue init;
obj.TryGetValue("name", out init);
Debug.WriteLine(init.ToString());
}
What this code does, is it goes through the existing objects in the array and gets the values stored for every specific object that meets the name requirement, printing them in the Debug console. The values are printed in quotes, so you need to take care of correct string handling, if the situation requires it. An interesting moment here is the fact that once an array or another object is set as a value, the returned value will be the string representation of that value.
Here is an example – I modified the existing JSON representation to include an array of secondary items:
string jsonString = @"{ ""parts"" : {
""part"" : [
{ ""name"" : [{ ""start"" : ""Main""},{ ""end"" : ""board""}] },
{ ""name"" : ""Keyboard"" },
{ ""name"" : ""CPU"" },
{ ""name"" : ""FAN"" }
] }
}";
If I run the above code, I will only obtain [{ ""start"" : ""Main""},{ ""end"" : ""board""}] as the returned value, since the developer-set nesting only covers the top level array.
The above code works good if you know what you are looking for. However, a bit of a different approach is used when the developer doesn’t know the object names.
string jsonString = @"{ ""parts"" : {
""part"" : [
{ ""unit1"" :""Mainboard"" },
{ ""unit2"" : ""Keyboard"" },
{ ""unit3"" : ""CPU"" },
{ ""unit4"" : ""FAN"" }
] }
}";
Here I have the same array, but different child objects. Being a key-value pair, I am going to use the code below to obtain both the key name and its value:
JsonObject jsonDoc = (JsonObject)JsonObject.Parse(jsonString);
JsonArray jsonArray = (JsonArray)jsonDoc["parts"]["part"];
foreach (JsonObject obj in jsonArray)
{
foreach (string key in obj.Keys)
{
Debug.WriteLine(key);
Debug.WriteLine(obj[key]);
}
}
I have the same declaration for JsonObject and JsonArray, however I am going through each key in the array (note, that I am ONLY going through the part array) and then get the value by passing the identifying key to the object.
As you can see from the code samples above, handling JSON data in Silverlight is simple, if you are aware of the document structure. Of course, you can adapt the samples to situations when the obtained data is represented in an unknown way, meaning that you are not aware of the naming structure as well as of the existing data elements (like arrays), but that will be a less-likely situation and it generally shows a poor design choice for the existing system.
Opinions expressed by DZone contributors are their own.
Comments