Json In C# Example
//create class
public class Grid
{
public string ColName;
public bool ColVisisblity;
public int ColDisplayIndex;
public int Colwidth;
public bool IsGroupCloumn;
public int PrintingColwidth;
}
//Paste this code in button click for Serialize
List eGrid = new List();
Grid g = new Grid();
g.ColDisplayIndex = 0;
g.ColName = "CatName";
g.ColVisisblity = true;
g.Colwidth = 250;
g.IsGroupCloumn = true;
g.PrintingColwidth = 110;
eGrid.Add(g);
Grid g1 = new Grid();
g1.ColDisplayIndex = 0;
g1.ColName = "CatID";
g1.ColVisisblity = false;
g1.Colwidth = 100;
g1.IsGroupCloumn = false;
g1.PrintingColwidth = 60;
g1.ColDisplayIndex = 0;
g1.ColName = "CatID";
g1.ColVisisblity = false;
g1.Colwidth = 100;
g1.IsGroupCloumn = false;
g1.PrintingColwidth = 60;
eGrid.Add(g1);
string ans = JsonConvert.SerializeObject(eGrid, Formatting.Indented);
textBox1.Text = ans;
//Paste this code in button click for Desalinize
List ab = JsonConvert.DeserializeObject
>(ans);
JSON - Evaluates to JavaScript Objects
Name:
Age:
Address:
Phone:
string ans = JsonConvert.SerializeObject(eGrid, Formatting.Indented);
textBox1.Text = ans;
//Paste this code in button click for Desalinize
List ab = JsonConvert.DeserializeObject
>(ans);
What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is
     lightweight text-data interchange format
- JSON is language
     independent *
- JSON is
     "self-describing" and easy to understand
JSON uses JavaScript syntax for describing data
objects, but JSON is still language and platform independent. JSON parsers and
JSON libraries exists for many different programming languages.
JSON - Evaluates to JavaScript Objects
The JSON text format is syntactically
identical to the code for creating JavaScript objects.
Because of this similarity,
instead of using a parser, a JavaScript program can use the built-in eval()
function and execute JSON data to produce native JavaScript objects.
Example Form W3School
JSON Object Creation in JavaScript
Name:
Age:
Address:
Phone:
Much like XML
- JSON is plain
     text
- JSON is
     "self-describing" (human readable)
- JSON is
     hierarchical (values within values)
- JSON can be
     parsed by JavaScript
- JSON data can be
     transported using AJAX
Much unlike XML
- No end tag
- Shorter
- Quicker to read
     and write
- Can be parsed
     using built-in JavaScript eval()
- Uses arrays
- No reserved
     words
Why JSON?
For AJAX applications, JSON is faster and easier than XML:
Using XML
- Fetch an XML
     document
- Use the XML DOM
     to loop through the document
- Extract values
     and store in variables
Using JSON
- Fetch a JSON
     string
- eval() the JSON
     string
JSON Syntax Rules
JSON syntax is a subset of the JavaScript object notation syntax:
- Data is in name/value
     pairs
- Data is
     separated by commas
- Curly braces
     hold objects
- Square brackets
     hold arrays
·        
JSON Name/Value Pairs
·        
JSON data is written as
name/value pairs.
·        
A name/value pair consists
of a field name (in double quotes), followed by a colon, followed by a value:
·        
"firstName" : "John"
·        
This is simple to
understand, and equals to the JavaScript statement:
·        
firstName = "John"
JSON Values
JSON values can be:
- A number
     (integer or floating point)
- A string (in
     double quotes)
- A Boolean (true
     or false)
- An array (in
     square brackets)
- An object (in
     curly brackets)
- null
JSON Objects
JSON objects are written inside curly brackets,
Objects can contain multiple name/values pairs:
{ "firstName":"John" ,
"lastName":"Doe" }
This is also simple to understand, and equals to
the JavaScript statements:
firstName = "John"
lastName = "Doe"
lastName = "Doe"
JSON Arrays
JSON arrays are written inside square brackets.
An array can contain multiple objects:
{
"employees":
"employees":
[
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}
In the example above, the object "employees" is an array containing three objects. Each object is a record of a person (with a first name and a last name).
In the example above, the object "employees" is an array containing three objects. Each object is a record of a person (with a first name and a last name).
JSON Uses JavaScript Syntax
Because JSON uses JavaScript syntax, no extra
software is needed to work with JSON within JavaScript.
With JavaScript you can create an array of
objects and assign data to it like this:
Example
var employees = [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName": "Jones" }
];
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName": "Jones" }
];
JSON Files
- The file type
     for JSON files is ".json"
- The MIME type
     for JSON text is "application/json"
 
No comments:
Post a Comment