JSON JavaScript Object Notation Thierry Sans Sending structured - - PowerPoint PPT Presentation

json javascript object notation
SMART_READER_LITE
LIVE PREVIEW

JSON JavaScript Object Notation Thierry Sans Sending structured - - PowerPoint PPT Presentation

JSON JavaScript Object Notation Thierry Sans Sending structured data How to send a structured data (arrays or dictionaries) through an HTTP request or response? Only strings are send back and forth Have a string representation of a


slide-1
SLIDE 1

JSON JavaScript Object Notation

Thierry Sans

slide-2
SLIDE 2

Sending structured data

How to send a structured data (arrays or dictionaries) through an HTTP request or response?

➡ Only strings are send back and forth ✓ Have a string representation of a complex data structure

function sendArray(){ var arr = [“1”,”2”,”3”] http.send('POST', /, arr); def saveArray (request): arr = request.GET['arg']

Request

Javascript object array Python object array must be a string!

slide-3
SLIDE 3

Why do we need JSON?

Original idea: using XML

✓ In practice: JSON is used for its simplicity

slide-4
SLIDE 4

The JSON standard (RFC 4627)

  • Lightweight open format to interchange data
  • Human readable
  • Used for serializing and transmitting structured data over a

network connection (HTTP mostly)

  • Since 2009 browsers support JSON natively

source http://en.wikipedia.org/wiki/JSON

slide-5
SLIDE 5

Anatomy of JSON

  • A JSON data structure is either

array (indexed array)

  • bject (associative array)
  • JSON values are

string - number - true - false - null

slide-6
SLIDE 6

JSON Array

image from http://www.json.org/

[ {"name": "Thierry"}, {"name": "Jeff"}, {"name": "Bill"}, {"name": "Mark"}, ] [1, 2, 3, 4, 5]

  • r
slide-7
SLIDE 7

JSON Object

image from http://www.json.org/

{ "firstName": "John", "lastName": "Smith", "age": 25, "male": true "address": { "streetAddress": "21 2nd Street", "additionalAddress": null "city": "New York", "state": "NY", "postalCode": "10021" } }

slide-8
SLIDE 8

JSON in Javascript (natively supported)

Serialization Javascript JSON Deserialization Javascript JSON

var myObject = JSON.parse(myJSONtext) var myJSONText = JSON.stringify(myObject);

slide-9
SLIDE 9

Serialization - Deserialization

js array

Request

JSON array

response

JSON array js array js array JSON array js array JSON array ...

Serialization Deserialization

frontend backend