This section covers three alternative, standardized formats for sending and receiving structured data: Extensible Markup Language (XML), JavaScript Object Notation (JSON), and Hypertext Markup Language (HTML).
XML
XML is a markup language for encoding and structuring data.
XML documents contain markup and content. Markup consists of tags, attributes, and elements. There are three types of tags: start-tags (<person>), end-tags (</person>), and empty-element tags (<noContact />). Empty-element tags are also referred to as self-closing tags. Attributes are key-value pairs within the start-tag or empty-element tag that provide additional information about the element.
Elements are the components that comprise an XML document. Elements are a collection of tags, attributes, and content. An element consists of both a start-tag and end-tag or an empty-element tag. The data between the start-tag and end-tags is the content. Content can contain markup, including other elements, which enables you to build parent-child relationships into your data structure. The following code snippet provides an example of an XML element.
<person>
<firstName>Nathan</firstName>
<lastName>Jones</lastName>
<emailAddress primary='true'>email@domain.com</emailAddress>
<noContact medium='email' />
</person>
JSON
JSON is a lightweight data format to exchange structured information.
JSON has a small, defined set of formatting rules that must be followed when creating payloads. Following are the supported data types and their associated formatting rules:
- Numbers: Unquoted.
- Boolean: true or false; unquoted.
- Strings: Double-quoted.
- Arrays: Comma-separated lists enclosed in square brackets.
- Objects: Collection of key:value pairs enclosed in curly braces.Objects are represented in Objective-C using NSDictionary.
- null: Unquoted.
The root type of properly formatted JSON documents is either an array or an object. The following code snippet is the JSON representation of the personcode example previously outlined in the XML overview.
{
"person": {
"firstName": "Nathan",
"lastName": "Jones",
"email": {
"emailAddress": "email@domain.com",
"primary": true
},
"noContact": "email"
}
}
HTML
HTML is a markup language standard for structuring data on a web page so that your browser can interpret it.
One key difference between HTML and XML document structures is that HTML documents have a predefined set of tag and attribute names.
<!DOCTYPE html>
<html>
<head>
<title>Person: Nathan Jones</title>
</head>
<body>
<div id='firstName'>Nathan</div>
<div id='lastName'>Jones</div>
<div id='emailAddress' data-primary='true'>
email@domain.com
</div>
<div id='noContact' data-medium='email' />
</body>
</html>