September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -2,4 +2,4 @@ Load a [[wp:JSON|JSON]] string into a data structure.
|
|||
Also create a new data structure and serialize it into JSON.
|
||||
|
||||
Use objects and arrays (as appropriate for your language)
|
||||
and make sure your JSON is valid (http://www.jsonlint.com/).
|
||||
and make sure your JSON is valid (http://www.jsonlint.com/ or https://jsonformatter-online.com/).
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
#import system.
|
||||
#import extensions.
|
||||
#import extensions'dynamic.
|
||||
import extensions.
|
||||
import extensions'dynamic.
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
#var json := "{ ""foo"": 1, ""bar"": [10, ""apples""] }".
|
||||
var json := "{ ""foo"": 1, ""bar"": [10, ""apples""] }".
|
||||
|
||||
#var o := json fromJson.
|
||||
var o := json fromJson.
|
||||
|
||||
console writeLine:"json.foo=":(o foo).
|
||||
console writeLine:"json.bar=":(o bar).
|
||||
console printLine("json.foo=",o foo).
|
||||
console printLine("json.bar=",o bar).
|
||||
].
|
||||
|
|
|
|||
1
Task/JSON/Gosu/json-1.gosu
Normal file
1
Task/JSON/Gosu/json-1.gosu
Normal file
|
|
@ -0,0 +1 @@
|
|||
gw.lang.reflect.json.Json#fromJson( String json ) : javax.script.Bindings
|
||||
20
Task/JSON/Gosu/json-2.gosu
Normal file
20
Task/JSON/Gosu/json-2.gosu
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"Name": "Dickson Yamada",
|
||||
"Age": 39,
|
||||
"Address": {
|
||||
"Number": 9604,
|
||||
"Street": "Donald Court",
|
||||
"City": "Golden Shores",
|
||||
"State": "FL"
|
||||
},
|
||||
"Hobby": [
|
||||
{
|
||||
"Category": "Sport",
|
||||
"Name": "Baseball"
|
||||
},
|
||||
{
|
||||
"Category": "Recreation",
|
||||
"Name": "Hiking"
|
||||
}
|
||||
]
|
||||
}
|
||||
3
Task/JSON/Gosu/json-3.gosu
Normal file
3
Task/JSON/Gosu/json-3.gosu
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var personUrl = new URL( "http://gosu-lang.github.io/data/person.json" )
|
||||
var person: Dynamic = personUrl.JsonContent
|
||||
print( person.Name )
|
||||
1
Task/JSON/Gosu/json-4.gosu
Normal file
1
Task/JSON/Gosu/json-4.gosu
Normal file
|
|
@ -0,0 +1 @@
|
|||
personUrl.JsonContent
|
||||
1
Task/JSON/Gosu/json-5.gosu
Normal file
1
Task/JSON/Gosu/json-5.gosu
Normal file
|
|
@ -0,0 +1 @@
|
|||
print( person.toStructure( "Person", false ) )
|
||||
28
Task/JSON/Gosu/json-6.gosu
Normal file
28
Task/JSON/Gosu/json-6.gosu
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
structure Person {
|
||||
static function fromJson( jsonText: String ): Person {
|
||||
return gw.lang.reflect.json.Json.fromJson( jsonText ) as Person
|
||||
}
|
||||
static function fromJsonUrl( url: String ): Person {
|
||||
return new java.net.URL( url ).JsonContent
|
||||
}
|
||||
static function fromJsonUrl( url: java.net.URL ): Person {
|
||||
return url.JsonContent
|
||||
}
|
||||
static function fromJsonFile( file: java.io.File ) : Person {
|
||||
return fromJsonUrl( file.toURI().toURL() )
|
||||
}
|
||||
property get Address(): Address
|
||||
property get Hobby(): List<Hobby>
|
||||
property get Age(): Integer
|
||||
property get Name(): String
|
||||
structure Address {
|
||||
property get Number(): Integer
|
||||
property get State(): String
|
||||
property get Street(): String
|
||||
property get City(): String
|
||||
}
|
||||
structure Hobby {
|
||||
property get Category(): String
|
||||
property get Name(): String
|
||||
}
|
||||
}
|
||||
4
Task/JSON/Gosu/json-7.gosu
Normal file
4
Task/JSON/Gosu/json-7.gosu
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
var person = Person.fromJsonUrl( personUrl )
|
||||
print( person.Name )
|
||||
print( person.Address.City )
|
||||
print( person.Hobby[0].Name )
|
||||
3
Task/JSON/Gosu/json-8.gosu
Normal file
3
Task/JSON/Gosu/json-8.gosu
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
print( person.toJson() ) // toJson() generates the Expando bindings to a JSON string
|
||||
print( person.toGosu() ) // toGosu() generates any Bindings instance to a Gosu Expando initializer string
|
||||
print( person.toXml() ) // toXml() generates any Bindings instance to standard XML
|
||||
1
Task/JSON/Gosu/json-9.gosu
Normal file
1
Task/JSON/Gosu/json-9.gosu
Normal file
|
|
@ -0,0 +1 @@
|
|||
var clone = eval( person.toGosu() )
|
||||
4
Task/JSON/Halon/json.halon
Normal file
4
Task/JSON/Halon/json.halon
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$data = json_decode(''{ "foo": 1, "bar": [10, "apples"] }'');
|
||||
|
||||
$sample = ["blue" => [1, 2], "ocean" => "water"];
|
||||
$jsonstring = json_encode($sample, ["pretty_print" => true]);
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
var data = JSON.parse('{ "foo": 1, "bar": [10, "apples"] }');
|
||||
|
||||
var sample = { "blue": [1,2], "ocean": "water" };
|
||||
var json_string = JSON.stringify(sample);
|
||||
|
|
@ -1 +0,0 @@
|
|||
var data = eval('(' + '{ "foo": 1, "bar": [10, "apples"] }' + ')');
|
||||
9
Task/JSON/MATLAB/json.m
Normal file
9
Task/JSON/MATLAB/json.m
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
>> jsondecode('{ "foo": 1, "bar": [10, "apples"] }')
|
||||
ans =
|
||||
struct with fields:
|
||||
|
||||
foo: 1
|
||||
bar: {2×1 cell}
|
||||
>> jsonencode(ans)
|
||||
ans =
|
||||
{"foo":1,"bar":[10,"apples"]}
|
||||
|
|
@ -4,5 +4,5 @@ var data = parseJson("""{ "foo": 1, "bar": [10, "apples"] }""")
|
|||
echo data["foo"]
|
||||
echo data["bar"]
|
||||
|
||||
var js = %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||
var js = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
|
||||
echo js
|
||||
|
|
|
|||
25
Task/JSON/Phix/json.phix
Normal file
25
Task/JSON/Phix/json.phix
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
--
|
||||
-- demo\rosetta\JSON.exw
|
||||
-- =====================
|
||||
--
|
||||
include builtins/json.e
|
||||
|
||||
puts(1,"roundtrip (10 examples):\n")
|
||||
sequence json_strings = {"{\"this\":\"that\",\"age\":{\"this\":\"that\",\"age\":29}}",
|
||||
"1",
|
||||
"\"hello\"",
|
||||
"null",
|
||||
"[12]",
|
||||
"[null,12]",
|
||||
"[]",
|
||||
"{\"this\":\"that\",\"age\":29}",
|
||||
"{}",
|
||||
"[null,[null,12]]"}
|
||||
|
||||
for i=1 to length(json_strings) do
|
||||
string s = json_strings[i]
|
||||
puts(1,s&"\n")
|
||||
object json_object = parse_json(s)
|
||||
puts(1,print_json("",json_object,true)&"\n")
|
||||
if not equal(print_json("",json_object,true),s) then ?9/0 end if
|
||||
end for
|
||||
|
|
@ -1,19 +1,19 @@
|
|||
import Foundation
|
||||
|
||||
let jsonString = "{ \"foo\": 1, \"bar\": [10, \"apples\"] }"
|
||||
if let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding) {
|
||||
if let jsonObject : AnyObject = NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments, error: nil) {
|
||||
println("NSDictionary: \(jsonObject)")
|
||||
}
|
||||
if let jsonData = jsonString.data(using: .utf8) {
|
||||
if let jsonObject: Any = try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) {
|
||||
print("Dictionary: \(jsonObject)")
|
||||
}
|
||||
}
|
||||
|
||||
let obj = [
|
||||
"foo": [1, "Orange"],
|
||||
"bar": 1
|
||||
]
|
||||
"foo": [1, "Orange"],
|
||||
"bar": 1
|
||||
] as [String : Any]
|
||||
|
||||
if let objData = NSJSONSerialization.dataWithJSONObject(obj, options: .PrettyPrinted, error: nil) {
|
||||
if let jsonString2 = NSString(data: objData, encoding: NSUTF8StringEncoding) {
|
||||
println("JSON: \(jsonString2)")
|
||||
}
|
||||
if let objData = try? JSONSerialization.data(withJSONObject: obj, options: .prettyPrinted) {
|
||||
if let jsonString2 = String(data: objData, encoding: .utf8) {
|
||||
print("JSON: \(jsonString2)")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
19
Task/JSON/Zkl/json-1.zkl
Normal file
19
Task/JSON/Zkl/json-1.zkl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
a,b:=Import("zklYAJL");
|
||||
var [const] YAJL=a, toJSON=b;
|
||||
src:=
|
||||
#<<<
|
||||
0'|{
|
||||
"pi": 3.14,
|
||||
"large number": 123456789123456791,
|
||||
"an array": [
|
||||
-1,
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
"foo"
|
||||
]
|
||||
}|;
|
||||
#<<<
|
||||
obj:=YAJL().write(src).close();
|
||||
// or obj:=src.pump(YAJL()).close(); // for example, from file or socket
|
||||
obj.println();
|
||||
2
Task/JSON/Zkl/json-2.zkl
Normal file
2
Task/JSON/Zkl/json-2.zkl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// using above code plus:
|
||||
toJSON(obj).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue