JSON object literal has the following characteristics:
{}
.
{key1:value,key2:value2}
that are separated by a colon :
and key must be a string and always placed inside double quotes and value must be valid JSON data type such as string, number, object, array, boolean, null, etc.
{
"name":"Smith", "salary":30000, "passion":"travelling"
}
When JSON object literal is surrounded by a single quote then it will be formed JSON string. Whenever a single quote is removed from the JSON string then it can be formed JSON object literal.
Below is an example of a JSON string.
'{"name":"Smith", "salary":30000, "passion":"travelling"}'
JSON object literal is exist inside JSON string:
{
"name":"Smith", "salary":30000, "passion":"travelling"
}
Please keep in mind that JSON is a string, not an object.
Javascript object can be created by JSON object literal.
Source Code
<script type="text/javascript">
users = {"name":"John", "salary":45000};
</script>
Javascript object is created by parsing JSON string. Let us see it.
Source Code
<script type="text/javascript">
var users = '{"name":"Smith", "salary":45000}';
jsObj = JSON.parse(users);
</script>
Object value can be access though the (.)
operator.
Source Code
<script type="text/javascript">
var users = '{"name":"Smith", "salary":45000}';
var obj = JSON.parse(users);
var userName = obj.name;
</script>
object values can also be access by using bracket ([]) notation.
Source Code
<script type="text/javascript">
var users = '{"name":"Smith", "salary":45000}';
var obj = JSON.parse(users);
var ActName = obj["name"];
</script>