Home Tutorials Training Consulting Books Company Contact us


Get more...

Training Events

JSON. This article explains JSON, the JavaScript Object Notification.

1. Using JSON

1.1. What is JSON?

JSON (JavaScript Object Notation) is an independent data exchange format designed for representing simple data structures. It is limited to text and numeric values; binary values are not supported.

JSON was originally derived as a subset of the JavaScript Specification (ECMAScript) and is therefore directly supported in JavaScript.

Data structures in JSON are based on key/value pairs. The key is a string, and the value can be a numerical value, a boolean value (true or false), or an object.

Although undefined by the standard, you should avoid using duplicate key names. Most JSON frameworks will override duplicate keys with the last value.

1.2. JSON Example

A JSON object is a set of key/value pairs which starts with "{" and ends with "}".

{
"firstName": "Lars",
"lastName": "Vogel",
"address": {
"street": "Examplestr.",
"number": "31"
}
}
Lists consist of one or more values surrounded by [] and separated by ",".
[[json_javascript]]
== JavaScript

The following is an example of a JSON object and its usage in JavaScript. You can evaluate the JSON via the eval function and then assign it to an object. This object can then be used in your JavaScript source code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<script type="text/javascript">
  // Evaluate the object and assign to variables
  var user = {
    "firstName": "Lars",
    "lastName": "Vogel",
    "address": {
      "street": "Examplestr.",
      "number": "31"
    }
  };
  // Use the object
  alert(user.firstName + ' lives on street ' + user.address.street);
  document.writeln(user.firstName + ' ' + user.lastName);
</script>
</body>
</html>
----
++++
<!-- Developer Media -->
<div id='ad-afterResources'>
    <script>
        googletag.cmd.push(function() { googletag.display('ad-afterResources'); });
    </script>
</div>
++++

2. Links and Literature

Not yet listed.

If you need more assistance we offer Online Training and Onsite training as well as consulting

Legal Privacy Policy Change consent