Working With JSON in C#

This article will cover the following:

  • What is JSON
  • How to create JSON string in C#
  • How to read JSON string in C#

What is JSON

JSON (JavaScript Object Notation) is standard design for human-readable data interchange. I think it is  ‘human-readable’ for developers. JSON works with a tree structure and it looks like a XML. It’s shorter and very easy to use. If you already have experience with XML, you will certainly learn easily.

JSON vs. XML

diffrence

Let’s examine this expression { "name": "Stephen Cousins" },

  1. If you’re using JSON, you must put your expression in “{ }” tags.
  2. “name” is the key.
  3. “Stephen Cousins” is a value of “name” key.

JSON has the logic like { “key”: “value” } statement.

And also JSON has the array.

Let’s take another expression and examine that:

  1. "students": [  
  2. "name""Stephen Cousins" },  
  3. "name""Austin A. Newton" },  
  4. "name""Adam Wilhite" },  
  5. "name""Enis Kurtay YILMAZ" }  
  6. ]  
  7. ”  
  1. “students” is the key.
  2. “[“ and “]”square brackets are array statement. It means there is the array between “[“ and “]” in brackets.
  3. All statements in square brackets are value of “students” key.
  4. As you see there are four arrays in square brackets and it represents four student names.

How to create JSON string in C#

Please create your new console project from Visual Studio.

Click File, New, Project, then Console Application (.NET Framework 3.5)

If you want to create or read a JSON string, you need a JSON Serialize or Deserialize.  So, please open your Solution Explorer in Visual Studio, right click on References, and then click “Manage NuGet Packages”.

nuget

Please search “Newtonsoft.JSON”on Nuget Package Manager and install it.

install

Please add “using Newtonsoft.Json;” statement. If you forget to add this statement, you can’t serialize any JSON strings.

Example JSON:
  1. JSON  
  2. {  
  3.     "universities":   
  4.     {  
  5.         "university""South Carolina State University",  
  6.         "students": [  
  7.           {  
  8.             "name""Stephen Cousins"  
  9.         }, {  
  10.             "name""Austin A. Newton"  
  11.         }, {  
  12.             "name""Adam Wilhite"  
  13.         }, {  
  14.             "name""Enis Kurtay YILMAZ"  
  15.         }]  
  16.     }  
  17. }  
Now, you need to create class for JSON. If you want to create easily, you can use the web site jsonutils.com.

jsonutils


Creating JSON string in C#:

C# Codes for Creating JSON string

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Newtonsoft.Json;  
  4.   
  5. namespaceEKY.CSharpCornerJSONArticle  
  6. {  
  7.     public class Student   
  8.     {  
  9.         public string name {  
  10.             get;  
  11.             set;  
  12.         }  
  13.     }  
  14.     public class Universities {  
  15.         public string university {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public IList < Student > students {  
  20.             get;  
  21.             set;  
  22.         }  
  23.     }  
  24.     public class ClassUniversities {  
  25.         public Universities universities {  
  26.             get;  
  27.             set;  
  28.         }  
  29.     }  
  30.     class Program  
  31.     {  
  32.         static void Main(string[] args)   
  33.         {  
  34.             Class Universities university1 = newClassUniversities();  
  35.   
  36.             university1.universities = newUniversities();  
  37.             university1.universities.university = "South Carolina StateUniversity";  
  38.   
  39.             List < Student > listStudent = newList < Student > ();  
  40.             Student student1 = newStudent {  
  41.                 name = "StephenCousins"  
  42.             };  
  43.             Student student2 = newStudent {  
  44.                 name = "Austin A. Newton"  
  45.             };  
  46.             Student student3 = newStudent {  
  47.                 name = "Adam Wilhite"  
  48.             };  
  49.             Student student4 = newStudent {  
  50.                 name = "Enis Kurtay YILMAZ"  
  51.             };  
  52.   
  53.             listStudent.Add(student1);  
  54.             listStudent.Add(student2);  
  55.             listStudent.Add(student3);  
  56.             listStudent.Add(student4);  
  57.   
  58.             university1.universities.students = listStudent;  
  59.             stringjson = JsonConvert.SerializeObject(university1);  
  60.   
  61.             Console.WriteLine(json);  
  62.             Console.ReadLine();  
  63.         }  
  64.     }  
  65. }  
Result

result

How to read JSON string in C#

I think this part will be very easy for you. You just need an example JSON string and JSON class of your example. You can use jsonutils.com again, if you want to create a class for your example JSON.

I will take JSON strings from my website.

Reading JSON string from website in C#:

C# Codes for reading JSON string from website
  1. using System;  
  2. using System.Collections.Generic;  
  3. using Newtonsoft.Json;  
  4. using System.Net;  
  5. using System.IO;  
  6.   
  7. namespace EKY.CSharpCornerJSONArticle  
  8. {  
  9.     public class Category  
  10.     {  
  11.         public int id {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string slug {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         public string title {  
  20.             get;  
  21.             set;  
  22.         }  
  23.         public string description {  
  24.             get;  
  25.             set;  
  26.         }  
  27.         public int parent {  
  28.             get;  
  29.             set;  
  30.         }  
  31.         public int post_count {  
  32.             get;  
  33.             set;  
  34.         }  
  35.     }  
  36.     public class Tag {  
  37.         public int id {  
  38.             get;  
  39.             set;  
  40.         }  
  41.         public string slug {  
  42.             get;  
  43.             set;  
  44.         }  
  45.         public string title {  
  46.             get;  
  47.             set;  
  48.         }  
  49.         public string description {  
  50.             get;  
  51.             set;  
  52.         }  
  53.         public int post_count {  
  54.             get;  
  55.             set;  
  56.         }  
  57.     }  
  58.     public class Author {  
  59.         public int id {  
  60.             get;  
  61.             set;  
  62.         }  
  63.         public stringslug {  
  64.             get;  
  65.             set;  
  66.         }  
  67.         public string name {  
  68.             get;  
  69.             set;  
  70.         }  
  71.         public string first_name {  
  72.             get;  
  73.             set;  
  74.         }  
  75.         public string last_name {  
  76.             get;  
  77.             set;  
  78.         }  
  79.         public string nickname {  
  80.             get;  
  81.             set;  
  82.         }  
  83.         public strin gurl {  
  84.             get;  
  85.             set;  
  86.         }  
  87.         public string description {  
  88.             get;  
  89.             set;  
  90.         }  
  91.     }  
  92.     public class CustomFields {}  
  93.     public class Post {  
  94.         public int id {  
  95.             get;  
  96.             set;  
  97.         }  
  98.         public string type {  
  99.             get;  
  100.             set;  
  101.         }  
  102.         public string slug {  
  103.             get;  
  104.             set;  
  105.         }  
  106.         public string url {  
  107.             get;  
  108.             set;  
  109.         }  
  110.         public string status {  
  111.             get;  
  112.             set;  
  113.         }  
  114.         public string title {  
  115.             get;  
  116.             set;  
  117.         }  
  118.         public string title_plain {  
  119.             get;  
  120.             set;  
  121.         }  
  122.         public string content {  
  123.             get;  
  124.             set;  
  125.         }  
  126.         public string excerpt {  
  127.             get;  
  128.             set;  
  129.         }  
  130.         public string date {  
  131.             get;  
  132.             set;  
  133.         }  
  134.         public string modified {  
  135.             get;  
  136.             set;  
  137.         }  
  138.         public IList < Category > categories {  
  139.             get;  
  140.             set;  
  141.         }  
  142.         public IList < Tag > tags {  
  143.             get;  
  144.             set;  
  145.         }  
  146.         public Author author {  
  147.             get;  
  148.             set;  
  149.         }  
  150.         public IList < object > comments {  
  151.             get;  
  152.             set;  
  153.         }  
  154.         public IList < object > attachments {  
  155.             get;  
  156.             set;  
  157.         }  
  158.         public int comment_count {  
  159.             get;  
  160.             set;  
  161.         }  
  162.         public stringc omment_status {  
  163.             get;  
  164.             set;  
  165.         }  
  166.         public CustomFields custom_fields {  
  167.             get;  
  168.             set;  
  169.         }  
  170.     }  
  171.     public class Query {  
  172.         public bool ignore_sticky_posts {  
  173.             get;  
  174.             set;  
  175.         }  
  176.     }  
  177.     public class ClassWebsiteposts {  
  178.         public string status {  
  179.             get;  
  180.             set;  
  181.         }  
  182.         public int count {  
  183.             get;  
  184.             set;  
  185.         }  
  186.         public int count_total {  
  187.             get;  
  188.             set;  
  189.         }  
  190.         public int pages {  
  191.             get;  
  192.             set;  
  193.         }  
  194.         public IList < Post > posts {  
  195.             get;  
  196.             set;  
  197.         }  
  198.         public Query query {  
  199.             get;  
  200.             set;  
  201.         }  
  202.     }  
  203.     class Program  
  204.     {  
  205.         static void Main(string[] args)   
  206.         {  
  207.             string url = "http://www.eniskurtayyilmaz.com/api/get_posts/";  
  208.   
  209.             HttpWebRequestrequest = WebRequest.Create(url) asHttpWebRequest;  
  210.             string jsonValue = "";  
  211.             using(HttpWebResponseresponse = request.GetResponse() asHttpWebResponse)  
  212.             {  
  213.                 StreamReaderreader = newStreamReader(response.GetResponseStream());  
  214.                 jsonValue = reader.ReadToEnd();  
  215.             }  
  216.   
  217.             ClassWebsitepostswebsitePosts = JsonConvert.DeserializeObject < ClassWebsiteposts > (jsonValue);  
  218.         }  
  219.     }  
  220. }  
Result

result

Read more articles on JSON: