Jump to content

JSON: Difference between revisions

642 bytes added ,  13 years ago
(added php)
Line 5:
=={{header|JavaScript}}==
Requires JSON library, now present in some browsers.
<lang JavaScript>var data = JSON.parse('{ "foo": 1, "bar": [10, "apples"] }');
 
var sample = { "blue": [1,2], "ocean": "water" };
var json_string = JSON.stringify(sample);</lang>
 
Of course, it IS called ''JavaScript'' Object Notation, so it is a JavaScript object literal,
and you can, alternately, parse it by just eval'ing it. This should work in any browser without a library.
(Eval may be dangerous, depending on the source of the data.)
However, there is an ambiguity with parsing JavaScript object literals by themselves, where it might be mistakenly interpreted as a block, and the key followed by a colon as a label. To avoid this, remember to surround it in parentheses to force it to be interpreted as an expression:
<lang javascript>var data = eval('(' + '{ "foo": 1, "bar": [10, "apples"] }' + ')');</lang>
 
=={{header|PHP}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.