1 / 5

Json ( javascript object notation)

420-B63 Programmation Web Avancée Auteur : Frédéric Thériault. Json ( javascript object notation). À quoi ça sert !?. Format d’échange entre différentes technologies. Sérialise les objets/tableaux/… en texte JSON en utilisant des symboles : [], :, {}, …

tyrone
Télécharger la présentation

Json ( javascript object notation)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 420-B63 Programmation Web Avancée Auteur : Frédéric Thériault Json(javascript object notation)

  2. À quoi ça sert !? • Format d’échange entre différentes technologies. • Sérialise les objets/tableaux/… en texte JSON en utilisant des symboles : [], :, {}, … • C’est une très bonne technique à utiliser pour le développement AJAX • PHP supporte JSON depuis 5.2. • Site Web : http://www.json.org/ • Il est fortement conseillé de travailler en UTF-8 avec JSON (pour le transport des accents).

  3. Exemple de texte JSON Le texte JSON suit le format standard de JavaScript : <script type="text/javascript"> varmycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; varjsonTexte = JSON.stringify(mycars); alert(jsonTexte); </script> Ceciaffichera : ["Saab","Volvo","BMW"] (c’est le texte JSON)

  4. Pour l’utiliser… • JavaScript : • Avoir intégré la librairie JSON à sa page Web, sauf dans certains navigateurs qui le supportent déjà. • VarobjTmp = JSON.stringify(texte)  Transforme en texte JSON • Var obj = JSON.parse(objTmp)  Transforme un texte JSON en entité JavaScript

  5. Pour l’utiliser… (suite) • PHP • En considérant un tableau comme suit : $maVariable = array(); $maVariable[] = array('John', 'Smith', 'Concierge'); $maVariable[] = array('Roger', 'Eddie', 'Joueur de hockey'); … • On appelle la fonction json_encode($maVariable) pour transformer le tableau en texte JSON echojson_encode($maVariable); • La fonction json_decode($variable) permet de construire un texte JSON en entité PHP.

More Related