Newtonsoft / JSON.Net
This is a third party tool created by Newtonsoft.com
link - newtonsoft.com/json
link - newtonsoft.com/json/help/html/JsonNetVsDotNetSerializers.htm
The most used classes are:
JsonConvert
[JsonProperty]
JsonReader
JsonWriter
JsonSerializer.
using Newtonsoft.Json;
SerializeObject
Creating JSON string in C#
public class Student
{
public string name {
get;
set;
}
}
public class Universities {
public string university {
get;
set;
}
public System.Collections.Generic.IList < Student > students {
get;
set;
}
}
public class ClassUniversities {
public Universities universities {
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
Class Universities university1 = new ClassUniversities();
university1.universities = new Universities();
university1.universities.university = "Warwick University";
System.Collections.Generic.List < Student > listStudent = newList < Student > ();
Student student1 = newStudent {
name = "First Name"
};
Student student2 = newStudent {
name = "Second Name"
};
Student student3 = newStudent {
name = "Third Name"
};
Student student4 = newStudent {
name = "Fourth Name"
};
listStudent.Add(student1);
listStudent.Add(student2);
listStudent.Add(student3);
listStudent.Add(student4);
university1.universities.students = listStudent;
stringjson = JsonConvert.SerializeObject(university1);
System.Console.WriteLine(json);
Console.ReadLine();
}
}
DeserializeObject
Reading JSON from a website
class Program
{
static void Main(string[] args)
{
string url = "http://bettersolutions.com/api/get_posts/";
HttpWebRequestrequest = WebRequest.Create(url) asHttpWebRequest;
string jsonValue = "";
using(HttpWebResponseresponse = request.GetResponse() asHttpWebResponse)
{
StreamReaderreader = newStreamReader(response.GetResponseStream());
jsonValue = reader.ReadToEnd();
}
ClassWebsitepostswebsitePosts = JsonConvert.DeserializeObject < ClassWebsiteposts > (jsonValue);
}
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev