This article was originally published on C# Corner in April 2016 (~2.9k reads). Reposted here on my own site.
This article covers what JSON is, how to create a JSON string in C#, and how to read a JSON string in C#.
What is JSON
JSON (JavaScript Object Notation) is a standard designed for human-readable data interchange. It works with a tree structure and looks similar to XML, but it is shorter and easier to use. If you already have experience with XML, you will learn it quickly.
JSON follows the logic of a { "key": "value" } statement, and it also supports arrays. Square brackets [ ] denote an array of values.
How to create a JSON string in C#
Create a new Console Application in Visual Studio, then install the Newtonsoft.Json NuGet package and add using Newtonsoft.Json;. Example JSON:
{
"universities": {
"university": "South Carolina State University",
"students": [
{ "name": "Stephen Cousins" },
{ "name": "Austin A. Newton" },
{ "name": "Adam Wilhite" },
{ "name": "Enis Kurtay YILMAZ" }
]
}
}Creating the JSON string in C#:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace EKY.CSharpCornerJSONArticle
{
public class Student
{
public string name { get; set; }
}
public class Universities
{
public string university { get; set; }
public IList<Student> students { get; set; }
}
public class ClassUniversities
{
public Universities universities { get; set; }
}
class Program
{
static void Main(string[] args)
{
ClassUniversities university1 = new ClassUniversities();
university1.universities = new Universities();
university1.universities.university = "South Carolina State University";
List<Student> listStudent = new List<Student>();
Student student1 = new Student { name = "Stephen Cousins" };
Student student2 = new Student { name = "Austin A. Newton" };
Student student3 = new Student { name = "Adam Wilhite" };
Student student4 = new Student { name = "Enis Kurtay YILMAZ" };
listStudent.Add(student1);
listStudent.Add(student2);
listStudent.Add(student3);
listStudent.Add(student4);
university1.universities.students = listStudent;
string json = JsonConvert.SerializeObject(university1);
Console.WriteLine(json);
Console.ReadLine();
}
}
}How to read a JSON string in C#
To deserialize, you just need an example JSON string and the matching C# classes. Example reading JSON from a URL:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Net;
using System.IO;
namespace EKY.CSharpCornerJSONArticle
{
public class Post
{
public int id { get; set; }
public string slug { get; set; }
public string title { get; set; }
public string content { get; set; }
public string date { get; set; }
}
public class ClassWebsiteposts
{
public string status { get; set; }
public int count { get; set; }
public IList<Post> posts { get; set; }
}
class Program
{
static void Main(string[] args)
{
string url = "https://www.eniskurtayyilmaz.com/api/get_posts/";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
string jsonValue = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
jsonValue = reader.ReadToEnd();
}
ClassWebsiteposts websitePosts = JsonConvert.DeserializeObject<ClassWebsiteposts>(jsonValue);
}
}
}