JSON (acrònim de JavaScript Object Notation) és un estàndard obert basat en text dissenyat per a intercanvi de dades llegible per humans. Deriva del llenguatge script JavaScript, per a representar estructures de dades simples i llistes associatives, anomenades objectes. Malgrat la seva relació amb el JavaScript, té implementacions per a gran part dels llenguatges de programació. Douglas Crockford va especificar i popularitzar el format JSON,[1] i es descriu a l'RFC 4627. El tipus MIME del JSON és application/json. L'extensió de fitxer és .json.
El format JSON s'utilitza habitualment per serialitzar i transmetre dades estructurades en una connexió de xarxa. S'utilitza principalment per intercanviar dades entre un servidor i una aplicació web, sent una alternativa a l'XML. S'utilitza freqüentment en aplicacions Ajax.
http://www.json.org/ http://www.json.org/json-es.html
application/json
Consulteu Java JSON
Vegeu GSON
Funcions de javascript:
Recursos:
Parser:
var obj = jQuery.parseJSON( '{ "name": "John" }' ); alert( obj.name === "John" )
Jquery no té un stringify per defecte. Es pot utilitzar el de javascript o algún plugin Jquery com:
https://code.google.com/p/jquery-json/
Peticions HTTP:
Exemple:
$("button").click(function(){ $.getJSON("demo_ajax_json.js",function(result){ $.each(result, function(i, field){ $("div").append(field + " "); }); }); });
http://es1.php.net/manual/es/ref.json.php
Exemple senzill:
Native encoding and decoding in browsers
Recent Web browsers now either have or are working on native JSON encoding/decoding. Not only does this eliminate the eval() security problem above, but also increases performance due to the fact that functions no longer must be parsed. Native JSON is generally faster compared to the JavaScript libraries commonly used before. As of June 2009 the following browsers have or will have native JSON support, via JSON.parse() and JSON.stringify():
Mozilla Firefox 3.5+[23] Microsoft Internet Explorer 8+[24] Opera 10.5+[25] WebKit-based browsers (Apple Safari)[26] Blink - based browsers (e.g. Google Chrome, Opera)
At least five popular JavaScript libraries have committed to use native JSON, if available:
YUI Library[27] Prototype[28] jQuery[29] Dojo Toolkit[30] MooTools[31]
The default character encoding for JSON is UTF8; it also supports UTF16 and UTF32.
TODO
JSON P s'utilitza a Javascript.
Resources:
Fake: