DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Working with JSON in .NET – a Silverlight example

Working with JSON in .NET – a Silverlight example

Denzel D. user avatar by
Denzel D.
·
Jun. 01, 10 · Interview
Like (0)
Save
Tweet
Share
22.06K Views

Join the DZone community and get the full member experience.

Join For Free

Unlike 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" : {
"part" : [
{ "name" : "Mainboard" },
{ "name" : "Keyboard" },
{ "name" : "CPU" },
{ "name"" : "FAN" }
] }
}
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.

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.

JSON Data structure Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases
  • OWASP Kubernetes Top 10
  • Create a REST API in C# Using ChatGPT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: