CS代考 RFC4627

www.cardiff.ac.uk/medic/irg-clinicalepidemiology

(JavaScript Object Notation)

Copyright By PowCoder代写 加微信 powcoder

Information modelling
& database systems

in the previous two lectures we learnt about XML, a markup language used to structure data
we pointed that XML format is “fat” and briefly mention JSON as a “slim” format that does the same job
in this lecture we will learn more about JSON

JSON = JavaScript Object Notation
pronounced like the name SON is a syntax for storing and exchanging data
text–based
light–weight
human readable
language independent
JSON is an open standard specified on RFC4627
https://www.ietf.org/rfc/rfc4627.txt

JavaScript is a high–level, dynamic, untyped and interpreted programming language
alongside HTML and CSS, JavaScript is one of the three essential technologies of the Web
programmers need an easy way to transfer data on the Web
JSON format is syntactically identical to the code for creating JavaScript objects
instead of using a parser (like XML does), JavaScript can use standard functions to convert JSON data into native objects, e.g. var json = JSON.parse(text);

A language is typed if the specification of every operation defines types of data to which the operation is applicable. In contrast, an untyped language allows any operation to be performed on any data, which are generally considered to be sequences of bits of various lengths.

JSON string
“name”: ” “,
“age”: 23,
“address”: {
“streetAddress”: “5 The Parade”,
“city”: “Cardiff”
“phoneNumber”: [
“type”: “home”,
“number”: “029 1234 5678”
“type”: “mobile”,
“number”: “077 8765 4321”
object starts
value: string
value: number
object starts

object ends
array starts
object starts

object ends
object starts

object ends
array ends
object ends

JSON object in JavaScript
var text = ‘{“name”: ” “, “age”: 23,
“address”: {“street”: “5 The Parade”, “city”:
“Cardiff”}, “phoneNumber”: [{“type”: “home”,
“number”: “029 1234 5678”}, {“type”: “mobile”,
“number”: “077 8765 4321”}]}’;

var json = JSON.parse(text);

alert(json.name); //
alert(json.address.street); // 5 The Parade
alert(json.address.city); // Cardiff
alert(json.phoneNumber[0].number); // 029 1234 5678 alert(json.phoneNumber[1].type); // mobile
try it online here

JSON and JavaScript
JSON is considered as a subset of JavaScript
… but that does not mean that JSON cannot be
used with other languages
JSON uses JavaScript syntax, but the JSON format is
text only… just like XML
JSON is language independent
it works well with most of the modern programming languages
e.g. PHP, Perl, Python, Ruby, Java and many more

JSON on the Web
serialization is the process of converting an object into a format suitable to be stored in a file or memory buffer and/or transmitted
JSON is often used to serialize and transfer data over a network connection
e.g. between web server and a web application
note: XML serves the same purpose!
Web services and APIs use JSON format to provide public data
e.g. Flickr and Twitter

JSON syntax
JSON syntax is derived from JavaScript object notation syntax:
data is in name/value pairs “name”:”value”
data is separated by commas ,
curly braces hold objects { object }
square brackets hold arrays [ array ]

JSON data is written as name/value pairs
a name/value pair consists of:
field name (in double quotes)
e.g. “firstName”:”John”

JSON values can be of the following data types:

Type Description Example
number double–precision floating–point format in JavaScript {“marks”: 97}
string double–quoted Unicode with backslash escaping {“name”: “John”}
Boolean true or false {name: “John”, marks: 97, distinction: true}
object an unordered collection of key:value pairs {name: “John”, marks: 97, distinction: true}
array an ordered sequence of values { “books”: [ {“title”:”Game” }, {title”:”Set”}, {“title”:”Match”} ] }
null empty

JSON objects are written inside curly braces
just like JavaScript, JSON objects can contain multiple name/values pairs, e.g.
{“firstName”:”John”, “lastName”:”Doe”}
JSON arrays are written inside square brackets
just like JavaScript, a JSON array can contain multiple objects, e.g.
“employees”:[
{“firstName”:”John”, “lastName”:”Doe”},
{“firstName”:”Anna”, “lastName”:”Smith”},
{“firstName”:”Peter”,”lastName”:”Jones”}

JavaScript
JSON syntax is derived from JavaScript object notation
in JavaScript, an array of objects can be created like this:
var employees = [
{“firstName”:”John”, “lastName”:”Doe”},
{“firstName”:”Anna”, “lastName”:”Smith”},
{“firstName”:”Peter”,”lastName”:”Jones”}
an element of the JavaScript object array can be accessed like this:
employees[0].firstName + ” ” +
employees[0].lastName;
employees[0][“firstName”] + ” ” + employees[0][“lastName”];

JavaScript
an element of the JavaScript object array can be modified like this:
employees[0].firstName = “Gilbert”;
employees[0][“firstName”] = “Gilbert”;
var employees = [
{“firstName”:”Gilbert”, “lastName”:”Doe”},
{“firstName”:”Anna”, “lastName”:”Smith”},
{“firstName”:”Peter”,”lastName”:”Jones”}

JSON within JavaScript
JSON syntax is derived from JavaScript object notation
very little extra software is needed to work with JSON within JavaScript
JSON is commonly used to read data from a web server, and display the data in a web page
for simplicity, we will demonstrate such use with a JSON string as input (instead of a file):
var text = ‘{ “employees” : [‘ +
‘{ “firstName”:”John” , “lastName”:”Doe” },’ +
‘{ “firstName”:”Anna” , “lastName”:”Smith” },’ +
‘{ “firstName”:”Peter”, “lastName”:”Jones” } ]}’;

JSON within JavaScript
the JavaScript function JSON.parse() can be used to convert a JSON string into a JavaScript object:
var obj = JSON.parse(text);
the new JavaScript object can now be used in the web page, e.g.

document.getElementById(“demo”).innerHTML =
obj.employees[1].firstName + ” ” +
obj.employees[1].lastName;

JSON Schema

“title”: “Example Schema”,
“type”: “object”,
“properties”: {
“firstName”: {“type”:”string”},
“lastName”: {“type”:”string”},
“description”:”Age in years”,
“type”:”integer”,
“minimum”:0
“required”: [“firstName”, “lastName”]
{“firstName”:”Peter”,”lastName”:”Pan”,”age”:12}

JSON Schema
JSON Schema is a specification for JSON–based format for defining the structure of JSON data
JSON Schema itself is written in JSON
schema is data itself, not a computer program
it is just a declarative format for “describing the structure of other data”
JSON data can be validated against a schema using a computer program
for documentation see: http://json-schema.org/

Hello, World!
in JSON Schema, an empty object is a valid schema that will accept any valid JSON, e.g.

accepts any valid JSON, e.g.

The type keyword
the most common thing to do in a JSON Schema is to restrict to a specific type, e.g.

only strings are accepted, e.g.

Declaring a JSON Schema
JSON Schema is itself JSON
it is not always easy to tell when something is JSON Schema or just JSON
the $schema keyword is used to declare that something is JSON Schema, e.g.
{ “$schema”: “http://json-schema.org/schema#” }
it is generally good practice to include it, though it is not required

Declaring a unique identifier
it is also good practice to include an id property
as a unique identifier for each schema, e.g.
{ “id”: “http://yourdomain.com/schemas/myschema.json” }

JSON Schema includes keywords:
title, description and default
not used for validation
used to describe parts of a schema
title will provide a short description
description will provide a more lengthy explanation about the purpose of the data described by the schema
neither are required, but they are encouraged as good practice
default specifies a default value for an item

Enumerated values
the enum keyword is used to restrict a value to a fixed set of values
it must be an array with at least one element, where each element is unique, e.g.

Enumerated values
enum can be used without a type, to accept values of different types, e.g.

Combining schemas
JSON schemas can be combined
this does not necessarily mean combining schemas from multiple files
it may be as simple as allowing data to be validated against multiple criteria
anyOf is used to say that the given data may be valid against any of the given sub–schemas
as long as a value validates against any of the sub–schemas, it is considered valid against the entire combined schema

Combining schemas
keywords used to combine schemas are:
allOf must be valid against all sub–schemas
anyOf must be valid against any subschema
oneOf must be valid against exactly one of the
sub–schemas
these keywords must be set to an array, where each item is a schema
in addition, there is:
not must not be valid against the given schema

JSON vs. XML

JSON vs. XML
{“students”:[
{“name”:”John”, “age”:”23″, “city”:”Cardiff”},
{“name”:”Steve”, “age”:”28″, “city”:”Swansea”},
{“name”:”Peter”, “age”:”32″, “city”:”Bristol”},

John 23 Cardiff

Steve 28 Swansea

Peter 32 Bristol

notice how the use an array
removes the need for the
nested “element”

JSON vs. XML
similarities
both are self–describing (human–readable)
both are hierarchical (values within values)
both can be parsed and used by many programming languages
both can be fetched with an XMLHttpRequest

differences
JSON does not use end tag
JSON is shorter
JSON is quicker to read and write
JSON can use arrays
biggest difference
XML has to be parsed with an XML parser
JSON can be parsed by a standard JavaScript function

JSON vs. XML
for AJAX applications, JSON is faster and easier than XML
fetch an XML document
use the XML DOM to traverse through the document
extract values and store in variables
using JSON
fetch a JSON string
JSON.Parse the JSON string

JSON vs. XML
there are several specifications to define schema for XML, e.g. DTD and XML Schema JSON Schema does the same for JSON, but it is not as widely used
for selecting specific parts of an XML document, there is standard specification called XPath JSONPath does the same for JSON, but is not as widely used
XML has XQuery specification for querying XML data JSON has JAQL, JSONiq etc, but they are not as widely used
XML has XSLT specification, which may be used to apply style to an XML document JSON does not have any such thing

/docProps/thumbnail.jpeg

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com