DEV Community

Cover image for Working with JSON in C#
TodayDeals.org
TodayDeals.org

Posted on • Edited on

Working with JSON in C#

JavaScript Object Notation (JSON) is a way of representing data structures in a “human-readable” form that can be used to exchange information between two different applications. Most commonly this is between client-side JavaScript running in a web browser, and a server-side script running on the web server. Although derived from JavaScript, JSON is a flexible format that can be used with many programming languages.

C# developers have a variety of structures, data formats, and mechanisms they can use to exchange information. The most common use of JSON for them is when communicating with a web service that only accepts and returns data in JSON format. It may be helpful to get an introduction to web APIs before continuing, which will provide background information as to why JSON is used and the benefits of working with web services. A fair amount of experience working with C# is assumed here, so it would be beneficial for beginners to learn programming in C# prior to attempting these techniques.

A Brief Overview of JSON

JSON represents objects in a structured text format, with data stored as key-value (or attribute-value) pairs, an example of which is shown below:
<script type="text/javascript">
var udemy = {
"name": "Media Relations",
"email": "press@udemy.com",
"notes": "Member of the media? We'd love to hear from you."
};
</script>

This script block was written for inclusion on a web page, and declares an object representing an entry in a simple address book or contacts list. From JavaScript, you access the information using a dot syntax like udemy.name or udemy.email. The code between the equals sign and the semicolon is what is referred to as JSON.

Because of the flexibility that JavaScript offers, it’s possible to declare a list of objects using one assignment statement:
<script type="text/javascript">
var contacts = [
{
"name": "Media Relations",
"email": "press@udemy.com",
"notes": "Member of the media? We'd love to hear from you."
},
{
"name": "Business Development",
"email": "bizdev@udemy.com",
"notes": "For general business development inquiries."
}
];
</script>

Notice how square brackets are used to mark the array of objects, and each object is surrounded by curly brackets and separated by a comma.

Udemy Coupon Codes
Get up to 100% discount for best Udemy courses with specially selected Udemy coupon codes that guarantee great savings.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

This is mis-tagged. #c is the tag for the C programming language, not C#.

Collapse
 
todaydealsorg profile image
TodayDeals.org

I just updated