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.



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