Login / Get an account Logout
  • view
  • edit
  • history
  • discuss

minimal/json

JSON = JavaScript Object Notation, syntaxe pour stocker et échanger des données

De l’objet au JSON

$ node
> let obj = { fullname: "J.C. Buisson", hobbies: ["tennis", "electronics"], weight: 73, height: 1.87 }
undefined
> let json = JSON.stringify(obj) ; json
{"fullname":"J.C. Buisson","hobbies":["tennis","electronics"],"weight":73,"height":1.87}

Du JSON à l’objet

$ node
> let json = '{ "fullname": "J.C. Buisson", "hobbies": ["tennis", "electronics"], "weight": 73, "height": 1.87 }'
undefined
> let obj = JSON.parse(json) ; obj
{ fullname: 'J.C. Buisson',
  hobbies: [ 'tennis', 'electronics' ],
  weight: 73,
  height: 1.87 }
> let json2 = "{ 'fullname': 'J.C. Buisson', 'hobbies': ['tennis', 'electronics'], 'weight': 73, 'height': 1.87 }"
undefined
> let obj2 = JSON.parse(json2)
undefined
> obj === obj2
true

ATTENTION : dans une chaîne JSON, les propriétés sont OBLIGATOIREMENT encadrées par des guillemets

$ node
> let json = "{ 'fullname': 'J.C. Buisson', 'hobbies': ['tennis', 'electronics'], 'weight': 73, 'height': 1.87 }"
undefined
> let obj = JSON.parse(json)
SyntaxError: Unexpected token ' in JSON at position 2
powered by gitit
Site
  • Front page
  • All pages
  • Categories
  • Random page
  • Recent activity
  • Upload a file
  • Help
This page
  • Raw page source
  • Printable version
  • Delete this page