Complete JSON tutorial: learn JSON quickly
Complete JSON tutorial: learn JSON quickly
JavaScript Object Notation, or the more commonly known abbreviation JSON, is widely used in various applications. Its purpose is to simplify communication, the most common is communication between the browser and the server. Although there are, the reverse is also true.
In your development process, you will inevitably encounter JSON. This article hopes to give you an understanding of how to use it.
A Basic Introduction
When you dive into JSON it is very easy to use, it is just plain text constructed in a certain way. If you've never seen it with your own eyes before, now is a good time to show a small example. This is what it looks like--
{
"SiteName": "CodeWall",
"ArticleName": "JSON Tutorials",
"Revision": 1
}
JSON Data Types:
JavaScript's object notation can handle almost all types of data you might need. This is of course a very powerful tool, and being able to handle arrays and objects on its own is great. Let's review the data types through some small examples and show how they look like in the running application.
String:
{
"StringDataType": "String"
}
Object:
{
"ObjectDataType": {
"myObj": "ObjectData"
}
}
Array
{
"ArrayDataType": [1, 2, 3, 4]
}
Number:
{
"NumberDataType": 1
}
Boolean:
{
"BooleanTrueDataType": true,
"BooleanFalseDataType": false
}
Date:
{
"DateStringDataType": "2011-07-14 19:43:37 +0100",
"JavaScriptDateDataType": new Date(1310669017000)
}
JSON Formatting:
Rule 1. Value & Key/Identifier
Rule 2. Key/Identifier Double Quoted
Rule 3. Wrap Objects In Curly Braces
Rule 4. Separate Key/Value by Colons
Rule 5. Arrays Are To Be Treated Like Arrays
Rule 6. Encapsulate Objects With Square Brackets (Array)
Rule 7. Objects & Data Separated By Commas
Basic Example:
- String
- Integer
- Decimal , Boolean.
{
"Article": "Tutorial",
"Author": "Dan Englishby",
"YearPublished": 2018,
"IsPublished": true,
"AverageWordsPerSentce": 20.3
}
Nesting:
{
"OuterObject": [{
"NestedObjects": [{
"SomeName": "SomeValue"
},
{
"SomeName": "SomeValue"
},
{
"SomeName": "SomeValue"
}
]
}]
}
{
"OuterObject": [{
"NestedArray": [
[
"SomeValue"
],
[
"SomeValue"
],
[
"SomeValue"
]
]
}]
}
Advance example 1
{
"Category": "Tutorials",
"Articles": [{
"Name": "ChartJS Lesson 1",
"Shared On": [{
"SocialNetwork": "Facebook"
},
{
"SocialNetwork": "Twitter"
},
{
"SocialNetwork": "Google+"
}
]
},
{
"Name": "ChartJS Lesson 2",
"Shared On": [{
"SocialNetwork": "Facebook"
},
{
"SocialNetwork": "Twitter"
},
{
"SocialNetwork": "Google+"
}
]
},
{
"Name": "ChartJS Lesson 3",
"Shared On": [{
"SocialNetwork": "Facebook"
},
{
"SocialNetwork": "Twitter"
},
{
"SocialNetwork": "Google+"
}
]
},
{
"Name": "ChartJS Lesson 4",
"Shared On": [{
"SocialNetwork": "Facebook"
},
{
"SocialNetwork": "Twitter"
},
{
"SocialNetwork": "Google+"
}
]
}
]
}
Advance Example 2
{
"Category": "Tutorials",
"Articles": [{
"Name": "ChartJS Lesson 1",
"Shared On": [
["Facebook"],
["Twitter"],
["Google+"]
]
}
]
}
Storing as a variable
var json = {
"Article": "Tutorial",
"Author": "Dan Englishby",
"YearPublished": 2018,
"IsPublished": true,
"AverageWordsPerSentce": 20.3
};
Comments
Post a Comment