End to End GUI Development with Qt5
上QQ阅读APP看书,第一时间看更新

JSON

In case you have never come across JSON before, let’s have a quick crash course. It is a simple and lightweight way to express hierarchies of objects and their properties. It is a very popular choice when sending data in HTTP requests. It is similar to XML in intent but is much less verbose.

A JSON object is encapsulated in curly braces {}, while properties are denoted in the format key: value. Strings are delimited with double quotes "". We can represent a single client object as follows:

{
"reference": "CLIENT0001", "name": "Dale Cooper" }

Note that white space and control characters such as tab and newline are ignored—the indented properties are to simply make things more readable.

It's usually a good idea to strip extraneous characters out of JSON when transmitting over the network (for example, in an HTTP request) in order to reduce the size of the payload; every byte counts!

Property values can be one of the following types: StringNumberJSON ObjectJSON Array, and the literal values true, false, and null.

We can add the supply address and billing address to our client as child JSON objects, providing a unique key for each. While keys can be in any format as long as they are unique, it is common practice to use camel case, for example, myAwesomeJsonKey. We can express an empty address object with null:

{
"reference": "CLIENT0001", "name": "Dale Cooper", "supplyAddress": { "number": 7, "name": "White Lodge", "street": "Lost Highway", "city": "Twin Peaks", "postcode": "WS119" }, "billingAddress": null }

A collection (array) of objects is enclosed in square brackets [] separated by commas. We can express no scheduled appointments by simply leaving the square brackets empty:

{
"reference": "CLIENT0001", "name": "Dale Cooper", "supplyAddress": { "number": 7, "name": "White Lodge", "street": "Lost Highway", "city": "Twin Peaks", "postcode": "WS119" }, "billingAddress": null, "contacts": [ { "type": 1, "address": "+12345678" }, { "type": 2, "address": "dale.cooper@fbi.com" } ], "appointments": [] }