vogella training Android Training Eclipse Training

What is JSON?

Lars Vogel

Version 1.1

22.05.2012

Revision History
Revision 0.1 31.05.2009 Lars
Vogel
created
Revision 0.2 - 1.1 13.06.2009 - 22.05.2012 Lars
Vogel
bug fixes and enhancements

JSON

This article explains JSON, the JavaScript Object Notification.


Table of Contents

1. JSON Introduction
2. Java Script
3. Thank you
4. Questions and Discussion
5. Links and Literature

1. JSON Introduction

JSON (JavaScript Object Notation) is an independent data exchange format. JSON is limited to text and numeric values. Binary values are not supported.

JSON is a subset of the JavaScript Specification (ECME-Script) and it is therefore directly supported in JavaScript.

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

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

			
{
 firstName:'Lars',
 lastName:'Vogel',
 address: {
 	street:'Examplestr.',
 	number: '31'
 }
}

		

Lists are one or more values surrounded by [] and separated by ",".

			
[
{
 firstName:'Lars',
 lastName:'Vogel',
 address: {
 	street:'Examplestr.',
 	number: '31'
 }
}
,
{
 firstName:'Jack',
 lastName:'Hack',
 address: {
 	street:'Examplestr.',
 	number: '31'
 }
}
]
		

2. Java Script

The following is an example of an JSON object and its usage in JavaScript. You evaluate the JSON via the function eval and can 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 in street ' + user.address.street); 
document.writeln(user.firstName + ' ' +user.lastName);
</script>
</body>
</html>
		

3. Thank you

Please help me to support this article:

Flattr this

4. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

5. Links and Literature

Not yet listed