JSON: Difference between revisions

978 bytes added ,  11 years ago
→‎{{header|OCaml}}: Using yojson lib
m (→‎{{header|C}}: indentation)
(→‎{{header|OCaml}}: Using yojson lib)
Line 944:
 
=={{header|OCaml}}==
 
=== Using json-wheel/json-static libs ===
 
{{libheader|json-wheel}}
Line 975 ⟶ 977:
let j = Json_io.json_of_string str in
print_endline (Json_io.string_of_json j);</lang>
 
=== Using yojson lib ===
 
{{libheader|yojson}}
 
<lang ocaml>open Yojson.Basic.Util
 
let s = "
{ \"name\": \"John Doe\",
\"pages\": [
{ \"id\": 1,
\"title\": \"The Art of Flipping Coins\",
\"url\": \"http://example.com/398eb027/1\"
},
{ \"id\": 2, \"deleted\": true },
{ \"id\": 3,
\"title\": \"Artichoke Salad\",
\"url\": \"http://example.com/398eb027/3\"
},
{ \"id\": 4,
\"title\": \"Flying Bananas\",
\"url\": \"http://example.com/398eb027/4\"
}
]
}"
 
let extract_titles json =
[json]
|> filter_member "pages"
|> flatten
|> filter_member "title"
|> filter_string
 
let () =
let json = Yojson.Basic.from_string s in
List.iter print_endline (extract_titles json)</lang>
 
Compile and run:
 
<pre>
$ ocamlfind ocamlopt -o filtering filtering.ml -package yojson -linkpkg
$ ./filtering
The Art of Flipping Coins
Artichoke Salad
Flying Bananas
</pre>
 
=={{header|OpenEdge/Progress}}==