diff options
Diffstat (limited to 'files/it/web/javascript/reference/global_objects/json/parse')
-rw-r--r-- | files/it/web/javascript/reference/global_objects/json/parse/index.html | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/global_objects/json/parse/index.html b/files/it/web/javascript/reference/global_objects/json/parse/index.html new file mode 100644 index 0000000000..f5c823ddf1 --- /dev/null +++ b/files/it/web/javascript/reference/global_objects/json/parse/index.html @@ -0,0 +1,126 @@ +--- +title: JSON.parse() +slug: Web/JavaScript/Reference/Global_Objects/JSON/parse +tags: + - ECMAScript5 + - JSON + - JavaScript + - Riferimento + - metodo +translation_of: Web/JavaScript/Reference/Global_Objects/JSON/parse +--- +<div>{{JSRef}}</div> + +<p><span class="seoSummary">Il metodo <strong><code>JSON.parse()</code></strong> analizza una stringa JSON, costruendo il valore JavaScript o l'oggetto descritto dalla stringa. È possibile fornire una funzione <strong>reviver</strong> opzionale per eseguire una trasformazione sull'oggetto risultante prima che venga restituito.</span></p> + +<div>{{EmbedInteractiveExample("pages/js/json-parse.html")}}</div> + + + +<h2 id="Sintassi">Sintassi</h2> + +<pre class="syntaxbox">JSON.parse(<var>text</var>[, <var>reviver</var>])</pre> + +<h3 id="Parametri">Parametri</h3> + +<dl> + <dt><code>text</code></dt> + <dd>La stringa da analizzare come JSON. Vedi l'oggetto {{jsxref("JSON")}} per una descrizione della sintassi JSON.</dd> + <dt><code>reviver</code> {{optional_inline}}</dt> + <dd>Se una funzione, questo prescrive come viene trasformato il valore originariamente prodotto dall'analisi, prima di essere restituito.</dd> +</dl> + +<h3 id="Valore_di_ritorno">Valore di ritorno</h3> + +<p>{{jsxref("Object")}} corrispondente al parametro JSON <code>text</code> dato.</p> + +<h3 id="Eccezione">Eccezione</h3> + +<p>Genera un errore {{jsxref("SyntaxError")}} se la stringa da analizzare non è JSON valida.</p> + +<h2 id="Esempi">Esempi</h2> + +<h3 id="Utilizzare_JSON.parse()">Utilizzare <code>JSON.parse()</code></h3> + +<pre class="brush: js">JSON.parse('{}'); // {} +JSON.parse('true'); // true +JSON.parse('"foo"'); // "foo" +JSON.parse('[1, 5, "false"]'); // [1, 5, "false"] +JSON.parse('null'); // null +</pre> + +<h3 id="Usare_il_parametro_reviver">Usare il parametro <code>reviver</code></h3> + +<p>Se viene specificato un <code>reviver</code>, il valore calcolato dall'analisi viene trasformato prima di essere restituito. In particolare, il valore calcolato e tutte le sue proprietà (che iniziano con le proprietà più nidificate e procedono al valore originale stesso) vengono eseguite individualmente attraverso il <code>reviver</code>. Quindi viene chiamato, con l'oggetto contenente la proprietà da elaborare come <code>this</code>, e con il nome della proprietà come stringa e il valore della proprietà come argomenti. Se la funzione <code>reviver</code> restituisce {{jsxref("undefined")}} (o non restituisce alcun valore, ad esempio, se l'esecuzione cade al termine della funzione), la proprietà viene cancellata dall'oggetto. In caso contrario, la proprietà viene ridefinita come il valore restituito.</p> + +<p>Se <code>reviver</code> trasforma solo alcuni valori e non altri, sii certo di restituire tutti i valori non trasformati così come sono, altrimenti verranno eliminati dall'oggetto risultante.</p> + +<pre class="brush: js">JSON.parse('{"p": 5}', (key, value) => + typeof value === 'number' + ? value * 2 // ritorna: value * 2 per i numeri + : value // restituisce tutto il resto invariato +); + +// { p: 10 } + +JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => { + console.log(key); // registra il nome della proprietà corrente, l'ultimo è "". + return value; // restituisce il valore della proprietà invariato. +}); + +// 1 +// 2 +// 4 +// 6 +// 5 +// 3 +// "" +</pre> + +<h3 id="JSON.parse()_non_consente_virgole_finali"><code>JSON.parse()</code> non consente virgole finali</h3> + +<pre class="example-bad brush: js example-bad">// both will throw a SyntaxError +JSON.parse('[1, 2, 3, 4, ]'); +JSON.parse('{"foo" : 1, }'); +</pre> + +<h2 id="Specifiche">Specifiche</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specifica</th> + <th scope="col">Stato</th> + <th scope="col">Commento</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.12.2', 'JSON.parse')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definizione iniziale. Implementato in JavaScript 1.7.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-json.parse', 'JSON.parse')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-json.parse', 'JSON.parse')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatiblità_con_i_browser">Compatiblità con i browser</h2> + +<div> + + +<p>{{Compat("javascript.builtins.JSON.parse")}}</p> +</div> + +<h2 id="Vedi_anche">Vedi anche</h2> + +<ul> + <li>{{jsxref("JSON.stringify()")}}</li> +</ul> |