aboutsummaryrefslogtreecommitdiff
path: root/files/it/web/javascript/reference/global_objects/json/parse/index.html
blob: f5c823ddf1a776ee2c980c8596dd8c494079825b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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) =&gt;
  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) =&gt; {
  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>