Introducing Payload Data Formats

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 contentMarkup 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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值