JSON Strings

JSON stands for JavaScript Object Notation
It is a lightweight format for storing and transporting data as text
It is often used when data is sent from a server to a web page
It is self-describing, easy to understand and it is often used as an alternative to XML.
The JSON format is syntactically identical to the code for creating JavaScript objects.


Name / Value Pairs

JSON data is written as name/value pairs, just like JavaScript object properties.
JSON names require double quotes.



Objects

{"firstName":"John", "lastName":"Doe"} 

This object has 2 keys "firstName" and "lastName".



Arrays

"staff":[ 
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]

"staff" is the key.
The "[" and "]" square brackets are an array statement. It means there is the array between them.
All statements in square brackets are values of "staff" key.



.NET Serializers

There are lots of different .NET serializers:
System.Text.Json
System.Runtime.Serialization.Json.DataContractJsonSerializer
System.Web.Script.Serialization.JavaScriptSerializer
Newtonsoft.Json / JSON.Net



Serializable Attribute

The purpose of this attribue is to advertise (for example to BinaryFormatter) that this class can be serialised.
Most serializers don't require this attribute.
For example, XmlSerializer, DataContractSerializer, JavaScriptSerializer or JSON.NET need this attribute.

[System.SerializableAttribute] 
[Serializable]
public class StaffClass


Converting JSON Text to an Object

You need to create class for JSON.
If you want to create easily, you can use the web site jsonutils.com.


Serialisation

more details


Dynamic Keys

JSON data is written as name/value pairs, just like JavaScript object properties.
Lets assume that we want to create a Person object that contains a First Name and a Last Name.
There are actually two different ways an object can be represented in JSON.


Format 1 - Array Format

This is the normal way:

"staff" : 
[
  {
    "id": 1,
    "firstName":"John",
    "lastName":"Doe"
  },
  {
    "id": 2,
    "firstName":"Anna",
    "lastName":"Smith"
  }
]

Format 2 - Dynamic Keys

However there is another format that can be used and this format uses a Dynamic Key.
In this format the "id" is used as the key.
This relies on the <key> to access a specific staff object.

"staff" : 
{
   "1" :
   {
      "firstName":"John",
      "lastName":"Doe"
   },
   "2" :
   {
      "firstName":"Anna",
      "lastName":"Smith"
   }
}

Equivalent Classes


Format 1 - Array Format

This is better because the StaffClass contains all the information about the staff member including the "id".

public class AllTheStaff 
{
    public System.Collections.Generic.List<StaffClass> staff { get; set; }
}

public class StaffClass
{
    public int id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
}

If you need to convert a List into a Dictionary you can use this:

System.Collections.Generic.Dictionary<string, StaffClass> StaffDictionary; 
StaffDictionary = AllTheStaff.StaffClass.ToDictionary(staff => staff.id);

Format 2 - Dynamic Keys


public class AllTheStaff 
{
    public System.Collections.Generic.Dictionary<string, StaffClass> StaffDictionary { get; set; }
}

public class StaffClass
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}




© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext