Jump to content

JSON: Difference between revisions

1,820 bytes added ,  13 years ago
Added PicoLisp
(added go)
(Added PicoLisp)
Line 43:
$json_string = json_encode($sample);
?></lang>
 
=={{header|PicoLisp}}==
PicoLisp has no JSON library, but it is easy enough to write one:
<lang PicoLisp>(de checkJson (X Item)
(unless (= X Item)
(quit "Bad JSON" Item) ) )
 
(de readJson ()
(case (read "_")
("{"
(make
(for (X (readJson) T (readJson))
(checkJson ":" (readJson))
(link (cons X (readJson)))
(T (= "}" (setq X (readJson))))
(checkJson "," X) ) ) )
("["
(make
(link T) # Array marker
(for (X (readJson) T (readJson))
(link X)
(T (= "]" (setq X (readJson))))
(checkJson "," X) ) ) )
(T @) ) )
 
(de printJson (Item) # For simplicity, without indentation
(cond
((atom Item) (print Item))
((=T (car Item))
(prin "[")
(map
'((X)
(printJson (car X))
(and (cdr X) (prin ", ")) )
(cdr Item) )
(prin "]") )
(T
(prin "{")
(map
'((X)
(print (caar X))
(prin ": ")
(printJson (cdar X))
(and (cdr X) (prin ", ")) )
Item )
(prin "}") ) ) )</lang>
This reads/prints JSON from/to files, pipes, sockets etc. To read from a string, a pipe can be used:
<pre>: (pipe (prinl "{ \"foo\": 1, \"bar\": [10, \"apples\"] }")
(readJson) )
-> (("foo" . 1) ("bar" T 10 "apples"))
 
: (printJson
(quote
("name" . "Smith")
("age" . 25)
("address"
("street" . "21 2nd Street")
("city" . "New York")
("state" . "NY")
("zip" . "10021") )
("phone" T "212 555-1234" "646 555-4567") ) )
{"name": "Smith", "age": 25, ... {"street": ... "phone": ["212 555-1234", ...</pre>
 
=={{header|Python}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.