JSON: Difference between revisions

From Rosetta Code
Content added Content deleted
(added php)
Line 5: Line 5:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
Requires JSON library, now present in some browsers.
Requires JSON library, now present in some browsers.
<lang JavaScript>var data = JSON.parse('{ "foo": 1, "bar": [10, "apples"] }')
<lang JavaScript>var data = JSON.parse('{ "foo": 1, "bar": [10, "apples"] }');


var sample = { "blue": [1,2], "ocean": "water" };
var sample = { "blue": [1,2], "ocean": "water" };
var json_string = JSON.stringify(sample);</lang>
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}}==
=={{header|PHP}}==

Revision as of 05:58, 25 August 2010

Task
JSON
You are encouraged to solve this task according to the task description, using any language you may know.

Load a JSON string into a data structure. Also create a new data structure and serialize it into JSON. Use objects and arrays, and make sure your JSON is valid (http://www.jsonlint.com/).

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>

PHP

<lang php><?php $data = json_decode('{ "foo": 1, "bar": [10, "apples"] }'); // dictionaries will be returned as objects $data2 = json_decode('{ "foo": 1, "bar": [10, "apples"] }', true); // dictionaries will be returned as arrays

$sample = array( "blue" => array(1,2), "ocean" => "water" ); $json_string = json_encode($sample); ?></lang>

Python

Works with: Python version 2.6+

<lang Python>import json data = json.loads('{ "foo": 1, "bar": [10, "apples"] }')

sample = { "blue": [1,2], "ocean": "water" } json_string = json.dumps(sample)</lang>